modified lenhist
[KandRexercises.git] / lenhist.c
blobce3f7a0f7b031fa5aa2b207a77020a807cfa3f24
1 #include <stdio.h>
3 /* exercise 1-13: create a histogram of the length of words in the input */
4 /* count the lengths of the words w/ an iterator
5 upon reading whitespace, increment the value saved at an array index equal to the iterator's value
6 reset iterator to 0 */
8 main()
10 int c, i, j, len;
11 int nlen[100];
13 len = 0;
14 for(i = 0; i < 100; ++i) {
15 nlen[i] = 0;
18 while((c = getchar()) != EOF) {
19 if(c == ' ' || c == '\n' || c == '\t') {
20 ++nlen[len];
21 len = 0;
22 } else {
23 len++;
27 for (i = 1; i < 100; ++i) {
28 if (nlen[i] > 0)
30 printf("%d: ", i);
31 for (j = 0; j < nlen[i]; ++j) {
32 printf("*");
34 printf("\n");