/* Warning! Not compiled or tested in any environment */
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXLENGTH 15
int main()
{
int c, i, j, word, length, overlong_words;
int word_length[MAXLENGTH+1];
for (i=0; i <= MAXLENGTH; ++i)
word_length[i] = 0;
word = OUT;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
{
if (word == IN)
{
if (length <= MAXLENGTH)
++word_length[length];
}
else
{
++overlong_words;
word = OUT;
}
length = 0; // Now reset word length
}
else {
if (word == OUT)
{
length = 0;
word = IN;
}
++length;
}
}
printf( "histogram:");
for (i = 0; i < MAXLENGTH; ++i) {
printf( "%2d", i);
for (j = 0; j < word_length[i]; ++j)
printf("*");
printf("\n");
}
}
I think that my logic here is fairly correct, and that`s why I`m a bit confused why it didn`t count the word lengths correctly.