#include /* * count the number of each digit, whitespace, * and all other chars */ void main(void) { register int c; /* input char */ register int nwhite = 0; /* whitespace count */ register int nother = 0; /* other count */ register int i; /* counter in a for loop */ int ndigit[10]; /* digit counts */ /* * initialize the ndigit array */ for(i = 0; i < 10; i++) ndigit[i] = 0; /* * handle input a char at a time */ while((c = getchar()) != EOF){ /* see what it is */ switch(c){ case '0': case '1': case '2': case '3': /* digit */ case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c - '0']++; break; case ' ': case '\t': case '\n': /* whitespace */ nwhite++; break; default: /* neither a digit nor whitespace */ nother++; break; } } /* * announce the results and quit */ printf("digits: "); for(i = 0; i < 10; i++){ printf("'%c' %3d\t", i + '0', ndigit[i]); /* put 5 digits per line, for neat output */ if (i == 4) printf("\n "); } putchar('\n'); printf("whitespace: %d\nother: %d\n", nwhite, nother); exit(0); }