题解 | #计算某字符出现次数#简单粗暴的C++实现
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream>
using namespace std;
//利用tolower函数实现字符串的字母全部小写
string ToLowerStr(string s) {
string lowerStr;
for (char c : s) {
lowerStr += tolower(c);
}
return lowerStr;
}
//遍历string,进行比较,注意:此时的目标字符也要全部小写
int CountChar(string s, char c) {
string lowerStr = ToLowerStr(s);
char lowerC = tolower(c);
int count = 0;
for (int i = 0; i < lowerStr.size(); i++) {
if (lowerStr[i] == lowerC) {
count++;
}
}
return count;
}
int main() {
string s;
char c;
getline(cin, s);
cin >> c;
int res = CountChar(s, c);
cout << res;
return 0;
}
// 64 位输出请用 printf("%lld")
