1.没有找到直接将字符串转换为大小写的方式,所以需要自己写一个函数。注意!tolower(s[i])有返回值,必须将其赋值给原字符串中的字符。
2.map中是自动按照键的字母序排序的(注意不是值的字母序)。
map的遍历: 使用迭代器,map<string, int>::iterator it, it->first访问的是键,it->second访问的是值。
int num = 0;
map<string, int> record;
void addTime(string x)
{
if (record.find(x) == record.end())
record[x] = 1;
else
record[x]++;
}
string shiftLower(string s)
{
for (int i = 0; i < s.length(); i++) {
if (isalpha(s[i]))
s[i] = tolower(s[i]);
}
return s;
}
int main()
{
string input;
getline(cin, input);
int index = 0, cnt = 0;
input += '\n';
char temp[100] = { '\0' };
while (index < input.length())
{
if (isalnum(input[index]) || isalpha(input[index]))
{
temp[cnt++] = input[index];
index++;
}
else
{
while (index < input.length() && !isalnum(input[index]) && !isalpha(input[index]))
{
index++;
}
temp[cnt] = '\0';
string str = temp;
str = shiftLower(str);
addTime(str);
cnt = 0;
}
}
int maxTime = 0;
string res;
map<string, int>::iterator it = record.begin();
for (; it != record.end(); it++)
{
if (it->second > maxTime)
{
maxTime = it->second;
res = it->first;
}
}
cout << res << " " << maxTime;
}