diff options
Diffstat (limited to 'one/count.c')
-rw-r--r-- | one/count.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/one/count.c b/one/count.c new file mode 100644 index 0000000..0f5bb82 --- /dev/null +++ b/one/count.c @@ -0,0 +1,41 @@ +#include <stdio.h> + +/* count digits, white space, and others */ +int +main() +{ + int c, i, nwhite, nother; + int ndigit[10]; + int nchar[26]; + nwhite = nother = 0; + + for (i = 0; i < 26; ++i) { + if (i < 10) { + ndigit[i] = 0; + } + nchar[i] = 0; + } + + while ((c = getchar()) != EOF) { + if (c >= '0' && c <= '9') { + ++ndigit[c - '0']; + } else if (c == ' ' || c == '\t' || c == '\n') { + ++nwhite; + } else if (c >= 'a' && c <= 'z') { + ++nchar[c - 'a']; + } else { + ++nother; + } + } + + printf("digits ="); + for (i = 0; i < 10; ++i) { + printf(" %d", ndigit[i]); + } + printf("\nchars ="); + for (i = 0; i < 26; ++i) { + printf(" %d", nchar[i]); + } + printf("\nwhite space = %d\nother = %d\n", nwhite, nother); + return 0; +} |