/* Example 11.1: Word lenght with star chart Author: Peter Brusilovsky */ #include #define MAXLEN 25 /* allow length up to 25 chars */ #define IN 1 #define OUT 0 int main() { int c, i, j, length, state; int counter[MAXLEN + 1]; /* initialization */ length = 0; state = OUT; for (i=0; i <= MAXLEN; ++i) counter[i] = 0; /* word counting */ while((c = getchar()) != EOF) if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) /* we are inside a word */ if(state == OUT) { /* it is the first letter of the word */ length = 1; state = IN; } else /* this is not the first letter of the word */ length ++; else /* we are outside a word */ if(state == IN) { /* just stepped out */ counter[length]++; state = OUT; } /* finding where to start report */ for (i = 25; i > 0 && counter[i] == 0; --i) ; /* reporting */ printf("\n ^\n"); for (; i >= 0; --i) { printf("%2d |", i); for(j=1; j <= counter[i]; ++j) printf("*"); printf("\n"); } printf(" +------------------>\n"); return 0; }