C++ String Manipulation

C++ code to count digits in a string

C++ programming language also provides a function called isdigit(int) that can be used to indicate whether a character in a string is a digit. This function takes one argument--the character(presented by its ascii value) that you want to check. In the example below we use the isdigit(int) function to count the number of digits in a string "C++ programming2011".


Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;

void countDigits(){
  string st="C++ programming2011";
  int count=0;
  for(int i=0;i<st.size();i++)
     if(isdigit(st[i])) count++;
  cout<<"Number of digits:"<<count;

}

int main(){

countDigits();
getch();
return 0;

}


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.