Word count histogram (horizontal)

Take a look at this:

#include 
main()
{
    int wlen[255]; //why would you have a word with more letters than this?
    int i,g,wordcount,lettercount,inner,c,maxVal,maxLet;
    wordcount=lettercount=maxVal=maxLet=0;

    for(i=0;i<=254;++i) wlen[i]=0; //zero out the array

    while ((c=getchar())!=EOF)
    {
        if ((c=='\n'||c=='\t'|| c==' ' || c=='.' || c==',' || c==';' || c==':')&& lettercount>0)
        //don't count double 'spacers' as zero length words
        {
            ++wordcount;
            wlen[lettercount]=wlen[lettercount]+1;
            if (lettercount>maxLet) maxLet=lettercount;
            if (wlen[lettercount]>maxVal) maxVal=wlen[lettercount];
            lettercount=0;
        }
        else ++lettercount;
     }
    for(i=maxVal;i>0;--i)
    {
        for (inner=0;inner<=maxLet;++inner)
        {
           if (wlen[inner]>=i) printf("X");
           else printf(" ");
        }
        printf("\n");
    }
     for (g=0;g<=maxLet;++g)  printf("%d",g);
      printf("\n\nHey Finished Man!!\n");
}

Leave a Reply