需要注意的是isalnum的返回值如果是则返回非零,否则返回零。
如果采用isalnum(i)==true的写法可能会得到错误的结果。
另外记录一下用到的cctype里的函数
isalpha()是不是字母
isdigit()是不是数字
isalnum()是不是字母数字
tolower()转换成小写
toupper()转换成大写
#include <iostream>
#include <map>
#include <cctype>
using namespace std;
map<string,int> m;
int main(){
string str,temp="";
getline(cin,str);
for(int i=0;i<str.length();i++){
if(isalnum(str[i])){
str[i]=tolower(str[i]);
temp=temp+str[i];
}
if(!isalnum(str[i])||i==str.length()-1){
if(temp.length()!=0){
m[temp]++;
temp="";
}
}
}
int index=0;
for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
if(it->second>index){
temp=it->first;
index=it->second;
}
}
cout<<temp<<" "<<index;
return 0;
}