fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /*
  5.  * This program prints the twenty most frequent words in a text file.
  6.  *
  7.  * Run on William Shakespeare's King Henry VIII, the top twenty words
  8.  * are:
  9.  *
  10.  * the 943
  11.  * and 735
  12.  * to 600
  13.  * i 579
  14.  * of 539
  15.  * a 457
  16.  * you 416
  17.  * my 371
  18.  * that 323
  19.  * in 298
  20.  * your 270
  21.  * his 268
  22.  * this 255
  23.  * me 233
  24.  * king 216
  25.  * for 210
  26.  * he 201
  27.  * is 193
  28.  * him 192
  29.  * not 191
  30.  *
  31.  */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35.  
  36. #define MAXWORDS 10000
  37. #define MAXSTRING 100
  38.  
  39. /* structure holding word frequency information */
  40.  
  41. typedef struct _word {
  42. char s[MAXSTRING]; /* the word */
  43. int count; /* number of times word occurs */
  44. } word;
  45.  
  46. /*
  47.  * This function inserts a word into the list of words. If the word is
  48.  * not yet in the list, a new slot is used with the count set to 1.
  49.  * Otherwise, the count is simply incremented.
  50.  */
  51. void insert_word (word *words, int *n, char *s) {
  52. int i;
  53.  
  54. /* linear search for the word */
  55. for (i=0; i<*n; i++) if (strcmp (s, words[i].s) == 0) {
  56.  
  57. /* found it? increment and return. */
  58.  
  59. words[i].count++;
  60. return;
  61. }
  62.  
  63. /* error conditions... */
  64.  
  65. if (strlen (s) >= MAXSTRING) {
  66. fprintf (stderr, "word too long!\n");
  67. exit (1);
  68. }
  69. if (*n >= MAXWORDS) {
  70. fprintf (stderr, "too many words!\n");
  71. exit (1);
  72. }
  73.  
  74. /* copy the word into the structure at the first available slot,
  75. * i.e., *n
  76. */
  77.  
  78. strcpy (words[*n].s, s);
  79.  
  80. /* this word has occured once up to now, so count = 1 */
  81.  
  82. words[*n].count = 1;
  83.  
  84. /* one more word */
  85.  
  86. (*n)++;
  87. }
  88.  
  89. /* comparison function for quicksort. this lets quicksort sort words
  90.  * by descending order of count, i.e., from most to least frequent
  91.  */
  92. int wordcmp (word *a, word *b) {
  93. if (a->count < b->count) return +1;
  94. if (a->count > b->count) return -1;
  95. return 0;
  96. }
  97.  
  98. /* return 1 if c is alphabetic (a..z or A..Z), 0 otherwise */
  99. int is_alpha (char c) {
  100. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') return 1;
  101. return 0;
  102. }
  103.  
  104. /* remove the i'th character from the string s */
  105. void remove_char (char *s, int i) {
  106. while (s[i]) {
  107. i++;
  108. s[i-1] = s[i];
  109. }
  110. s[i] = 0;
  111. }
  112.  
  113. /* remove non-alphabetic characters from the string s */
  114. void remove_non_alpha (char *s) {
  115. int i;
  116.  
  117. for (i=0; s[i]; i++) if (!is_alpha (s[i])) remove_char (s, i);
  118. }
  119.  
  120. /* make all the letters in s lowercase */
  121. void make_lowercase (char *s) {
  122. int i;
  123.  
  124. for (i=0; s[i]; i++) s[i] = tolower (s[i]);
  125. }
  126.  
  127. /* main program */
  128. int main () {
  129. word words[MAXWORDS];
  130. char s[1000];
  131. int i, n, m;
  132.  
  133. n = 0;
  134.  
  135. /* read all the words in the file... */
  136.  
  137. while (!feof (stdin)) {
  138. scanf ("%s", s);
  139.  
  140. /* only insert the word if it's not punctuation */
  141.  
  142. if (is_alpha (s[0])) {
  143.  
  144. /* get rid of non-letters */
  145.  
  146. remove_non_alpha (s);
  147.  
  148. /* make all letters lowercase */
  149.  
  150. make_lowercase (s);
  151.  
  152. /* put this word in the list */
  153.  
  154. insert_word (words, &n, s);
  155. }
  156. }
  157.  
  158. /* sort the list of words by descending frequency */
  159.  
  160. qsort((void *) words, n, sizeof (word),
  161. (int (*) (const void *, const void *)) wordcmp);
  162.  
  163.  
  164. /* if fewer than 20 words in total, just print up the the
  165. * first n words
  166. */
  167. if (n < 20)
  168. m = n;
  169. else
  170. m = 20;
  171.  
  172. /* print the words with their frequencies */
  173.  
  174. for (i=0; i<m; i++)
  175. printf ("%s\t%d\n", words[i].s, words[i].count);
  176. }
  177.  
Success #stdin #stdout 0s 4372KB
stdin
The Life of King Henry the Eighth
Shakespeare homepage | Henry VIII | Entire play
ACT I
PROLOGUE

    I come no more to make you laugh: things now,
    That bear a weighty and a serious brow,
    Sad, high, and working, full of state and woe,
    Such noble scenes as draw the eye to flow,
    We now present. Those that can pity, here
    May, if they think it well, let fall a tear;
    The subject will deserve it. Such as give
    Their money out of hope they may believe,
    May here find truth too. Those that come to see
    Only a show or two, and so agree
    The play may pass, if they be still and willing,
    I'll undertake may see away their shilling
    Richly in two short hours. Only they
    That come to hear a merry bawdy play,
    A noise of targets, or to see a fellow
    In a long motley coat guarded with yellow,
    Will be deceived; for, gentle hearers, know,
    To rank our chosen truth with such a show
    As fool and fight is, beside forfeiting
    Our own brains, and the opinion that we bring,
    To make that only true we now intend,
    Will leave us never an understanding friend.
    Therefore, for goodness' sake, and as you are known
    The first and happiest hearers of the town,
    Be sad, as we would make ye: think ye see
    The very persons of our noble story
    As they were living; think you see them great,
    And follow'd with the general throng and sweat
    Of thousand friends; then in a moment, see
    How soon this mightiness meets misery:
    And, if you can be merry then, I'll say
    A man may weep upon his wedding-day.

SCENE I. London. An ante-chamber in the palace.

    Enter NORFOLK at one door; at the other, BUCKINGHAM and ABERGAVENNY 

BUCKINGHAM

    Good morrow, and well met. How have ye done
    Since last we saw in France?

NORFOLK

    I thank your grace,
    Healthful; and ever since a fresh admirer
    Of what I saw there.
stdout
and	15
the	12
a	12
of	8
to	7
that	6
as	6
may	6
see	6
i	5
we	5
they	5
in	5
you	4
be	4
play	3
come	3
make	3
now	3
such	3