我得到了一個代碼。它應該給我一個輸出,可以擦除'z'和'p'之間的中間字符。例如:zipZap( 「zipXzap」):預期[zpXzp]但發現[Z PXZ P]字符串錯誤輸出
std::string zipZap(const std::string& str){
string a = str;
string b = "";
size_t len = str.length();
for (size_t i = 0; i < len; i++){
if (str[i] == 'z')
if (str[i+2] == 'p')
a[i+1] = ' ';
}
return a;
}
在i取代了第[i + 1] = '';它給了我一個錯誤。
它做了你想做的事情。刪除z和p之間的字符。 –
好吧,它不會刪除任何字符。它用空格替換它們。 –
你的代碼有'a [i + 1] =''',它將*空格*放在那裏,而不是刪除現有的字符。 – crashmstr