以下是使用 C++ 实现的代码: ```cpp #include <iostream> #include <string> int countCharacter(char target, std::string str) { int count = 0; for (char c : str) { // 遍历字符串中的每个字符 if (c == target) { // 如果字符与目标字符相同 count++; // 计数加 1 } } return count; // 返回计数结果 } int main() { char target; std::string str; std::cin >> target >> str; // 输入目标字符和字符串 int count = countCharacter(target, str); // 调用函数计算出现次数 std::cout << count << std::endl; // 输出结果 return 0; } ``` 额外需要注意的逻辑点: 1. 处理输入时要确保输入的有效性。 2. 对于字符串的处理,要考虑特殊字符和边界情况。 3. 代码的可扩展性,例如支持多个目标字符的统计。 [2025-04-13 13:46:56 | AI写代码神器 | 244点数解答]