题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream>
#include <string>
#include <cctype> // 包含字符处理函数tolower
int main() {
std::string inputString;
char targetChar;
// 输入字符串
getline(std::cin, inputString);
// 输入目标字符
std::cin >> targetChar;
// 统计字符出现次数(不区分大小写)
int count = 0;
char lowercaseTargetChar = tolower(targetChar); // 转换目标字符为小写
for (char c : inputString) {
if (tolower(c) == lowercaseTargetChar) {
count++;
}
}
// 输出结果
std::cout << count << std::endl;
return 0;
}