/* * Program to count how many of each character in input. */ #include #include #include int main(void) { printf("enter some text, control-D to end\n"); int inchar; int total = 0; /* UCHAR_MAX defined in limits.h -- largest value for char */ int counters[UCHAR_MAX+1] = { 0 }; while ((inchar = fgetc(stdin)) != EOF) { total += 1; counters[inchar] += 1; fputc(inchar, stdout); } printf("%d chars total\n", total); for (int i = 0; i < UCHAR_MAX+1; ++i) { if (isprint(i)) { printf("'%c' occurs %d times\n", i, counters[i]); } else { printf("char with value %d occurs %d times\n", i, counters[i]); } } return 0; }