fork download
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main(void)
  6. {
  7. enum { SIZE = 256 };
  8.  
  9. unsigned lengths[SIZE] = { 0 };
  10.  
  11. int ch;
  12. unsigned word_length = 0;
  13. while ((ch = getchar()) != EOF)
  14. {
  15. if (!isalnum(ch))
  16. {
  17. if (word_length >= SIZE)
  18. {
  19. /* handle error */
  20. }
  21. else
  22. ++lengths[word_length];
  23.  
  24. word_length = 0;
  25. }
  26. else
  27. ++word_length;
  28. }
  29.  
  30. if (word_length) /* handle case where EOF immediately follows a word */
  31. {
  32. if (word_length >= SIZE)
  33. {
  34. /* handle error */
  35. }
  36. ++lengths[word_length];
  37. }
  38.  
  39. unsigned largest_count = 0;
  40. unsigned longest_index = 0;
  41. for (unsigned i = 0; i < SIZE; ++i)
  42. {
  43. if (lengths[i])
  44. {
  45. longest_index = i;
  46.  
  47. if (largest_count < lengths[i])
  48. largest_count = lengths[i];
  49. }
  50. }
  51.  
  52. const char* asterisks = "********************";
  53. size_t asterisks_length = strlen(asterisks);
  54. double ratio = ((double)asterisks_length) / longest_index;
  55.  
  56. for (unsigned i = 1; i <= longest_index; ++i)
  57. printf("%3u: %.*s\n", i, (unsigned)(ratio*lengths[i]), asterisks);
  58. }
Success #stdin #stdout 0s 2056KB
stdin
`Twas brillig, and the slithy toves
  Did gyre and gimble in the wabe:
All mimsy were the borogoves,
  And the mome raths outgrabe.

"Beware the Jabberwock, my son!
  The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
  The frumious Bandersnatch!"

He took his vorpal sword in hand:
  Long time the manxome foe he sought --
So rested he by the Tumtum tree,
  And stood awhile in thought.

And, as in uffish thought he stood,
  The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
  And burbled as it came!

One, two! One, two! And through and through
  The vorpal blade went snicker-snack!
He left it dead, and with its head
  He went galumphing back.

"And, has thou slain the Jabberwock?
  Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!'
  He chortled in his joy.

`Twas brillig, and the slithy toves
  Did gyre and gimble in the wabe;
All mimsy were the borogoves,
  And the mome raths outgrabe. 
stdout
  1:  *
  2:  ********************
  3:  ********************
  4:  ********************
  5:  ********************
  6:  ********************
  7:  ********************
  8:  ********
  9:  *****
 10:  ******
 11:  
 12:  *