exc/k&r/01-a-tutorial-introduction/01-14.c

42 lines
781 B
C

#include <stdio.h>
#define CHARS 254
/* prints character frequency histogram */
void main()
{
int i, c;
int counts[CHARS];
/* initializing */
for (i = 0; i < CHARS; ++i)
counts[i] = 0;
/* counting */
while ((c = getchar()) != EOF) {
++counts[c];
}
/* printing */
for (c = 0; c < CHARS; ++c) {
if (counts[c] == 0)
continue;
if (c == '\n')
printf("\\n ");
else if (c == ' ')
printf("' ' ");
else if (c == '\t')
printf("\\t ");
else if (c == '\b')
printf("\\b ");
else
printf("%c ", c);
for (i = 0; i < counts[c]; ++i)
putchar('#');
putchar('\n');
}
}