第29355题 单选题
已知一段存在边界错误且可优化的C++字符统计函数,以下哪个选项是对其正确的修正与优化后的实现?

原始错误代码如下:

int countTargetChar(const string& str, char target) {
    int count = 0;
    for(int i = 0; i <= str.length(); ++i) {
        if(str[i] == target) {
            ++count;
        }
    }
    return count;
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析