這個特殊的任務與刪除字符串中的子字符串有關;我正在嘗試一些斯坦福大學SEE在線課程來學習一些新的語言。C++字符串操作對我來說沒有意義
到目前爲止,我已經有了下面是什麼,但如果text = "hello hello"
和remove ="el"
時,它會停留在循環,但如果我改變文本text = "hello hllo"
,它的工作原理,讓我覺得我做一些明顯的愚蠢。
有一個在分配不修改輸入的字符串,而是返回一個新的字符串的規定。
string CensorString1(string text, string remove){
string returned;
size_t found=0, lastfound=0;
found = (text.substr(lastfound,text.size())).find(remove);
while (string::npos != found){
returned += text.substr(lastfound,found);
lastfound = found + remove.size();
found = (text.substr(lastfound,text.size())).find(remove);
}
returned += text.substr(lastfound,found);
return returned;
}
指導,將不勝感激:-)感謝
UPDATE
接過給出的非常客氣的建議和修改我的代碼如下:
string CensorString1(string text, string remove){
string returned;
size_t found=0, lastfound=0;
found = text.find(remove);
while (string::npos != found){
returned += text.substr(lastfound,found);
lastfound = found + remove.length();
found = text.find(remove,lastfound);
}
returned += text.substr(lastfound);
return returned;
}
但還是表現的同樣
任何MO重新創意人?
謝謝!更新問題以反映 – Bolster 2010-04-25 18:19:10
@Andrew:也已更新。 – Vlad 2010-04-25 18:37:55
@Vlad,* facepalm *總是諮詢原型......謝謝,享受打勾和upvote – Bolster 2010-04-25 19:23:02