/** * digits - Count the number of digit characters, whitespace characters, and * other characters from standard input. * * Based on a program found on p. 22 of the 2nd edition of K&R. * Updated a bit by Samuel A. Rebelsky. * Annotated by the members of CSC 161 2011S. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // Access the stuff in the stdio library, such // as printf // +------+----------------------------------------------------------- // | Main | // +------+ // Main is where we start all programs. int main () { // Declare a bunch of integer variables int c; // The current character read from stdin. int i; // Counter variable for loops int nwhite; // The number of whitespace characters int nother; // The number of "other" characters int ndigit[10]; // The number of digit characters. // ndigit[0] gives the number of '0's, etc. // Initialize everything to 0: Our starting point. We haven't seen // any characters. nwhite = 0; nother = 0; for (i = 0; i < 10; i++) ndigit[i] = 0; // Repeatedly // Read a character // Assign it to c // Stop when you reach end of file while ((c = getchar ()) != EOF) { // If the current character is a digit if ((c >= '0') && (c <= '9')) // Increment the counter associated with that digit ++ndigit[c-'0']; // If the current character is a blank or a newline or a tab else if ((c == ' ') || (c == '\n') || (c == '\t')) ++nwhite; // Otherwise, it's something else else ++nother; } // while ndigit[9] = 23412; // Display our counts printf ("digits ="); // printf ("%d %d %d %d %d %d %d %d ...", ndigits[0], ndigits[1], ...) for (i = 0; i < 10; i++) printf(" %02d", ndigit[i]); printf(", whitespace = %d, other = %d\n", nwhite, nother); return 0; } // main