题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
int ch_of_cnt(const string& str,const char& ch)
{
int cnt = 0;
for (char c : str) {
if (isalpha(c) && tolower(c) ==tolower(ch)) {
cnt++;
} else if (c == ch) {
cnt++;
}
}
return cnt;
}
int main() {
string str1,str2 ;
getline(cin,str1);
getline(cin,str2);
char ch = str2.front();
cout << ch_of_cnt(str1,ch) <<endl;
}
// 64 位输出请用 printf("%lld")
