#include <iostream.h>
// Define a function which returns true (1) if the parameter character
// is in the ascii range 48 to 57 (ie a digit), false (0) if it is not
int isADigit(char c)
{
if(c >= 48 && c <= 57)
{
return 1;
}
else
{
return 0;
}
}
// main tests the function
void main ( )
{
// declare variables
char buffer[80];
int digit_count;
char answer;
// loop until the user terminates the program
do
{
digit_count = 0;
cout << "Enter a string of characters";
cin >> buffer;
// loop through the string, counting digits
for(int i = 0; buffer[i] != '\0'; i++)
{
if(isADigit(buffer[i]))
{
digit_count++;
}
}
// Display the result
cout << "Total number of digits in string = " << digit_count
<< endl;
cout << "Try another string (Y/N)?";
cin >> answer;
cout << endl;
} while (answer != 'n' && answer != 'N');
}