Print a histogram of the length of the words input.

Source:

/* print the input out as a histogram for the number of letters per word. */
/* word defined by letters surrounded by whitespace. */
#include "stdio.h"

#define TRUE 1
#define FALSE 0

typedef signed char bool; 

int main(int argc, char *argv[])
{
  int character = ' ';
  bool in_word = FALSE;
  bool running = TRUE;
  int word_length[1024];
  int which_word = 0;

  printf("Type fewer than 1024 words. I will print a histogram showing the length of each word.\n[Enter] to submit. EOF (ctrl+d) to quit.\n");
  printf("----------------------------------------------------------------------------------\n");

  while(running)
  {
    character = getchar();

    if(character == '\n' || character == ' ' || character == '\t' || character == EOF)
    {
      if(in_word)
      {
        /* we are no longer in a word.*/
        in_word = FALSE;

        /* move on to the next word.*/
        which_word++;

        /* format to the next line */
        /*printf("\n");*/
      }
      /* otherwise do nothing, we don't to print excessive new lines/tabs/spaces. */
    }
    else
    {
      in_word = TRUE;
      /* printf("%c", character);*/
      /* increment the word length count for that word. */
      word_length[which_word]++;
    }
    /* Test for EOF to quit here.*/
    if(character == EOF)
    {
      printf("\n");
      running = FALSE;
    }
  }

  printf("----------------------------------------------------------------------------------\n");

  int i=0;

  int histogram[27] = {0}; 

  /* match up the number of letters for each word for the histogram. */
  for(; i<which_word; i++)
  {
    switch(word_length[i])
    {
      case 0:
        break;

      case 1:
        histogram[1]++;
        break;

      case 2:
        histogram[2]++;
        break;

      case 3:
        histogram[3]++;
        break;

      case 4:
        histogram[4]++;
        break;

      case 5:
        histogram[5]++;
        break;

      case 6:
        histogram[6]++;
        break;

      case 7:
        histogram[7]++;
        break;

      case 8:
        histogram[8]++;
        break;

      case 9:
        histogram[9]++;
        break;

      case 10:
        histogram[10]++;
        break;

      case 11:
        histogram[11]++;
        break;

      case 12:
        histogram[12]++;
        break;

      case 13:
        histogram[13]++;
        break;

      case 14:
        histogram[14]++;
        break;

      case 15:
        histogram[15]++;
        break;

      case 16:
        histogram[16]++;
        break;

      case 17:
        histogram[17]++;
        break;

      case 18:
        histogram[18]++;
        break;

      case 19:
        histogram[19]++;
        break;

      case 20:
        histogram[20]++;
        break;

      case 21:
        histogram[21]++;
        break;

      case 22:
        histogram[22]++;
        break;

      case 23:
        histogram[23]++;
        break;

      case 24:
        histogram[24]++;
        break;

      case 25:
        histogram[25]++;
        break;

      case 26:
        histogram[26]++;
        break;

      default:
        printf("too big\n");
        break;
    }
  }

  int o = 0;

  /* print out our histogram. */
  for(i=1; i<26; i++)
  {
    /* first print the number of each line. */
    printf("%i\t|", i);

    /* then print the frequency using some ascii art. */
    for(; o<histogram[i]; o++)
    {
      printf("=");
    }
    /* format for the next line. */
    printf("\n");

    /* reset o. */
    o = 0;
  }

  printf("----------------------------------------------------------------------------------\n");
  return 0;
}

Output:

Type fewer than 1024 words. I will print a histogram showing the length of each word.
[Enter] to submit. EOF (ctrl+d) to quit.
----------------------------------------------------------------------------------
Please print a histogram of these words. It will be very neat!
----------------------------------------------------------------------------------
1	|=
2	|===
3	|
4	|====
5	|===
6	|=
7	|
8	|
9	|=
10	|
11	|
12	|
13	|
14	|
15	|
16	|
17	|
18	|
19	|
20	|
21	|
22	|
23	|
24	|
25	|
----------------------------------------------------------------------------------