word which occurs most

  • arminho1
  • Newbie
  • Newbie
  • arminho1
  • Posts: 6

Post 3+ Months Ago

Hello i want to write a program that takes the following input:
-- Many lines, each contains one word,followed by end.
Then it prints out the word that occurs most frequently.

For example, if the input looks like this:

the
boy
and
the
girl
end

The program will print "the" because this word occurs twice.
How can i do this?
My code so far
Code: [ Select ]
#include <stdio.h>
#include <string.h>

int main()
{
    char name[20];
    char words[20][20];
  int i;
    i=0;

    fgets(name,20,stdin);
    while (strncmp(name,"end",3) != 0)
    {    
        strcpy(words[i],name);
        fgets(name, 20, stdin);
        i++;    
  }
    printf("n = %s\n", words[1]);

}
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     char name[20];
  6.     char words[20][20];
  7.   int i;
  8.     i=0;
  9.     fgets(name,20,stdin);
  10.     while (strncmp(name,"end",3) != 0)
  11.     {    
  12.         strcpy(words[i],name);
  13.         fgets(name, 20, stdin);
  14.         i++;    
  15.   }
  16.     printf("n = %s\n", words[1]);
  17. }

thank you
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • billstack
  • Newbie
  • Newbie
  • billstack
  • Posts: 5

Post 3+ Months Ago

Howdy,

The following piece of code will do what you want. It is a modification of an example originally written by Fred Swartz (hence the comment at the top). I found this online, and then modified it slightly to do what you want.

It makes heavy use of STL, which may not be what you are looking for.

Cheers!

Code: [ Select ]
// Word frequencies -- using map
// Fred Swartz 2001-12-11

#include <map>
#include <string>
#include <iostream>

using namespace std;

int main(void)
{
  map<string, int> histogram;
  string word;
  int max;

  while (cin >> word)
  {
    histogram[word]++;

    if (word == "end")
      break;
  }

  max = -1;
  map<string, int>::iterator iter;
  for(iter=histogram.begin(); iter!=histogram.end(); iter++)
  {
    if (iter->second > max)
    {
      max = iter->second;
      word = iter->first;
    }
  }

  cout << word << " = " << max << endl;

  return 0;
}
  1. // Word frequencies -- using map
  2. // Fred Swartz 2001-12-11
  3. #include <map>
  4. #include <string>
  5. #include <iostream>
  6. using namespace std;
  7. int main(void)
  8. {
  9.   map<string, int> histogram;
  10.   string word;
  11.   int max;
  12.   while (cin >> word)
  13.   {
  14.     histogram[word]++;
  15.     if (word == "end")
  16.       break;
  17.   }
  18.   max = -1;
  19.   map<string, int>::iterator iter;
  20.   for(iter=histogram.begin(); iter!=histogram.end(); iter++)
  21.   {
  22.     if (iter->second > max)
  23.     {
  24.       max = iter->second;
  25.       word = iter->first;
  26.     }
  27.   }
  28.   cout << word << " = " << max << endl;
  29.   return 0;
  30. }

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 100 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.