Huawei trial-word count, Huawei word count
Enter an article, and output the words with the highest frequency and the secondary frequency (all in lower case, separated by commas ). Only the space, comma, and period delimiters are displayed in the article.
Regardless of the frequency. Case Insensitive.
Input: I am a student. I come from XiDian, I love XiDian.
Output: I, xidian
#include<iostream>#include<string>#include<map>#include<cctype>using namespace std;int main(int argc, char *argv[]){ string s; map<string,int>m; while(getline(cin,s)!=NULL) { string t; for(int i=0;i<s.size();++i) { if(s[i]==' '||s[i]==','||s[i]=='.') { m[t]+=1; t=""; } else t+=s[i]; } } int first=0,second=0; string s1,s2; for(map<string,int>::iterator it=m.begin();it!=m.end();++it) { if(it->second>first) { s1=it->first; first=it->second; } if(it->second>second&&it->second<first) { s2=it->first; second=it->second; } } for(int i=0;i<s1.size();++i) { cout<<(char)tolower(s1[i]); } cout<<","; for(int i=0;i<s2.size();++i) { cout<<(char)tolower(s2[i]); } cout<<"\n"; return 0;}