我收到了一個代碼。它假設每次發現「code」,「cope」,「coze」,「cole」或「core」時,都會給出一個計數值的輸出。例如:countCode(「aaacodebbb」)它應該是1,但發現爲0.substr的錯誤輸出
int countCode(const string& inStr) {
int count = 0;
for (unsigned i = 0; i < inStr.length(); i++) {
if (inStr.substr(i,i+3) == "code" || inStr.substr(i,i+3) == "coze" || inStr.substr(i,i+3) == "cope" || inStr.substr(i,i+3) == "core" || inStr.substr(i,i+3) == "cole") {
count++;
}
}
return count;
}
你試過打印(或在調試器中檢查)'inStr.substr(i,i + 3)'嗎? – John3136