Friday, 25 September 2015

Program to find word whose occurrence is maximum times in a sentence using Map STL

#include <string>
#include <iostream>
#include <map>
#include <utility>
using namespace std;

int main()
{
  map<string,int> ana;
string d,s[100],s1[100];
int i=0,j=0;
while(cin>>s[i])
{
s1[j++]=s[i++];
}

for( i=0;i<j;i++)
{
ana[s1[i]]++ ;
}

map<string,int>::iterator ii;
int max=ana.begin()->second;
d=ana.begin()->first;
for(ii =ana.begin(); ii!=ana.end(); ++ii)
{
  if(ii->second >max)
  {
  max=ii->second;
  d=ii->first;
  }
}

cout<<"\n Word '"<<d<<"' Occurs max times("<<max <<")";
    return 0;
}

No comments:

Post a Comment