/* * count the number of each digit, whitespace, * and all other chars * * Matt Bishop, ECS 36A * April 12, 2024 original version */ #include <stdio.h> int 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 */ if (c >= '0' && c <= '9'){ /* it's a digit -- bump the right count */ ndigit[c - '0']++; } else if (c == ' ' || c == '\t' || c == '\n'){ /* it's whitespace */ nwhite++; } else{ /* it's neither a digit nor whitespace */ nother++; } } /* * 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); }
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |