华为机试:计算某字符出现次数
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
string str;
char ch;
getline(cin, str);//读取含空格的字符串
cin >> ch;
map<char, int> m;
for(char c:str)
{
if(m.find(tolower(c)) != m.end())//tolower小写函数
{
m[tolower(c)]++;
}
else
{
m.insert(make_pair(tolower(c),1));
}
}
cout << m[tolower(ch)] << endl;
return 0;
}
腾讯成长空间 5958人发布