Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Exercise 1-14.
- * Write a program to print a histogram of the
- * frequencies of different characters in its input.
- */
- #include <stdio.h>
- #define CHARCOUNT 52
- #define HWIDTH 80
- main() {
- int c, chrs[CHARCOUNT], i;
- int count, max;
- /* initialize the histogram and max */
- for (i = 0; i < CHARCOUNT; i++)
- chrs[i] = 0;
- max = 0;
- /* populate the histogram */
- while ((c = getchar()) != EOF) {
- if (c >= 'a' && c <= 'z') {
- ++chrs[c - 'a'];
- } else if (c >= 'A' && c <= 'Z') {
- ++chrs[c - 'A' + 26];
- }
- }
- /* calculate max */
- for (i = 0; i < CHARCOUNT; i++)
- if (chrs[i] > max)
- max = chrs[i];
- /* draw the histogram */
- for (i = 0; i < CHARCOUNT; i++) {
- if (chrs[i]) {
- /* is it a small character */
- if (i < 26)
- c = i + 'a';
- else
- c = i - 26 + 'A';
- printf("[%c] ", c);
- for (count = 0; count < (chrs[i]*HWIDTH)/max; count++)
- putchar('@');
- for (count = 0;
- count < HWIDTH - (chrs[i]*HWIDTH)/max; count++)
- putchar(' ');
- printf(" %6d\n", chrs[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement