题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
map<char , int> mp ;
char ch ;
char t ;
while(getline(cin, s)){
cin>>ch ; // 不会读取换行符
cin>> t;
// cout<<s.size()<<endl ;
for(int i =0 ; i < s.size() ; ++ i)
{
mp[s[i]] +=1 ;
// cout<<s[i] <<endl ;
}
if(mp.find(ch) != mp.end()) // 如果找到了
{
if(((ch - 'a') < 32) && ((ch - 'a') >= 0) ){ // 如果是小写字母
if(mp.find(ch- 32) != mp.end())
{
cout<<mp[ch-32] + mp[ch] <<endl ;
}
else{
cout<<mp[ch] <<endl ;
}
}
else if(((ch - 'A') < 32) && ((ch - 'A') >= 0) ){ // 如果是大写字母
if(mp.find(ch + 32) != mp.end()){
cout<< mp[ch] + mp[ch + 32] <<endl ;
}
else{
cout<< mp[ch] <<endl;
}
}
else{ // 如果是其他字符的话。
cout<<mp[ch] <<endl ;
}
}
else
{
cout<<0<<endl ;
}
}
}
// 64 位输出请用 printf("%lld")