/* prints a histogram of the frequencies of different characters in * input */ #include #define MAXHIST 10.0 int main(void) { int longest = 0; int c, i, val; int chars['z'-'a'+1]; for(i = 0; i < 'z'-'a'+1; i++) chars[i] = 0; /* get input */ while ((c=getchar()) != EOF) { if (isalpha(c)) { val = tolower(c) - 'a'; chars[val]++; longest = (chars[val] > longest) ? chars[val] : longest; } } /* draw histogram */ for (c = MAXHIST; c > 0; c--) { putchar('\n'); for (i = 0; i < 'z'-'a'+1; i++) if (chars[i] >= ((longest/MAXHIST)*c) && chars[i] > 0) printf("# "); else printf(" "); } /* draw footer */ putchar('\n'); for (i = 0; i < 'z'-'a'+1; i++) if (chars[i] > 0) printf("---"); putchar('\n'); for (i = 0; i < 'z'-'a'+1; i++) if (chars[i] > 0) printf("%-3c", i + 'a'); putchar('\n'); return 0; }