C++ String manipulation isn't making sense to me...
- by Andrew Bolster
I am trying some of the Stanford SEE courses online to learn some new languages; this particular assignment has to do with removing substrings from strings.
What I've got so far is below, but if text = "hello hello" and remove ="el", it gets stuck in a loop, but if i change text to text = "hello hllo", it works, making me think I'm doing something obviously stupid.
There is a stipulation in the assignment not to modify the incoming strings, and instead to return a new string.
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;
}
Guidance would be appreciated :-) Thanks