#include /* using printf, fgets, stdin, BUFSIZ */ #include /* using exit */ #include /* using strlen */ int main() { int count = 0; // Count the number of numeric digits. char input[BUFSIZ]; char * promptPartA = "Please enter lines of text, followed by\n"; char * promptPartB = "an empty line (just ) to exit:\n"; printf("%s%s", promptPartA, promptPartB); while (fgets (input, BUFSIZ, stdin)) /* fgets returns NULL if EOF */ { int i; int stringLength = strlen(input); /* Remove newline if it is there by replacing it with a null byte. */ if ( stringLength > 0 && input[stringLength-1] == '\n' ) { input[stringLength-1] = '\0'; stringLength--; } /* On Windows, there might also be a carriage return; remove it too. */ if ( stringLength > 0 && input[stringLength - 1] == '\r') { input[stringLength-1] = '\0'; stringLength--; } /* If there is no input, we're done. */ if ( stringLength == 0 ) { break; // break out of loop } /* Include characters in the range of '0' - '9' in the count. */ for ( i = 0; i < stringLength; i++ ) { if ( input[i] >= '0' && input[i] <= '9' ) { count++; } } } /* Display the count of numeric characters. */ printf("\nThere were %d numeric characters in the input.\n", count); exit(0); }