#include #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ #define LARGEST 10 /* The largest word we keep track of */ /** * histowords - Generate a histogram of word lengths, more or less. * * To do: * Make it a visual histogram, rather than a numeric histogram. * Deal with the last word in the input when EOF falls after it. * Deal with words of length greater than LARGEST. * Improve the output. */ int main() { int c; // The next input character int state; // Are we in or out of a word. int len; // The length of the current word int lengths[LARGEST+1] = { 0 }; int i,j; // Traditional counter variables for loops len = 0; state = OUT; while ((c = getchar()) != EOF) { if (c == ' ' || c == '\n' || c == '\t') { if (state == IN) { lengths[len]++; } state = OUT; len = 0; } // if (c is whitespace) else if (state == OUT) { state = IN; len = 1; } // if (state == OUT) else { ++len; } // in a word } // while printf ("Len Num Pictoral\n"); for (i = 1; i < LARGEST+1; i++) { printf ("%2d: %3d: ", i, lengths[i]); for (j = 0; j < lengths[i]; j++) printf ("*"); printf ("\n"); } // for each length return 0; } // main