0

i am learning c language with a book authored by K&R. This code should print a vertical histogram of the lengths of words in its input but outputs nothing. i'm not sure what is really going on.

#include <stdio.h>

#define MAXWORLD 20 /* Maximum length of a word */
#define MAXNO 20 /* Maximum Number of words in a sentence */

int main(void) {

    int word[MAXNO];
    int i, c, j, nc, nw; /* c: last character input; nc: character counts; nw: word counts */

    for (i = 0; i < MAXNO; ++i)
    {
        word[i] = 0;
    }

    nc = nw = 0;

    while( (c=getchar()) != EOF)
    {
        ++nc;
        if( c ==' ' || c =='\n' || c =='\t')
        {
            /* -1 for excluding the space in the word length */
            word[nw] = nc -1;
            ++nw;
            /* resetting the word-length for the next word */
            nc = 0;
        }
    }

    for( i = MAXWORLD; i >= 1; --i)
    {
        for(j=0;j <= nw;++j)
        {
            if( i <= word[j])
                putchar('*');
            else
                putchar(' ');
        }
        putchar('\n');
    }
}

Please, someone should explain.

0 Answers0