Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <string.h>
- #define MAX_WORDS 50
- #define MAX_WORD_LEN 25
- using namespace std;
- struct HashItem
- {
- char *str;
- int count;
- };
- int add_word(char *w, HashItem *list, int sz)
- {
- for(int i=0; i<sz; i++)
- if(strcmpi(w, list[i].str) == 0)
- {
- list[i].count += 1;
- return 0;
- }
- list[sz].str = w;
- list[sz].count = 1;
- return 1;
- }
- HashItem get_max(HashItem *list, int sz)
- {
- HashItem item;
- int max = -1;
- for(int i=0; i<sz; i++)
- if(list[i].count > max)
- {
- item = list[i];
- max = item.count;
- }
- return item;
- }
- int main(int argv, char **argc)
- {
- HashItem *list = new HashItem[MAX_WORDS];
- int sz = 0;
- // Sample Input
- char in_str[] =
- "foo bar baz "
- "int foo bar foo";
- char *str = in_str;
- char *token = NULL;
- while(token = strtok(str, " "))
- {
- str = NULL;
- sz += add_word(token, list, sz);
- }
- // See the list
- for(int i=0; i<sz; i++)
- cout<<list[i].str<< ", " <<list[i].count<<endl;
- // Obtain Max
- HashItem item = get_max(list, sz);
- cout<<"Max occurrence: "<< item.str << " ("<<item.count<<")";
- delete list;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement