lundi 27 avril 2015

C++ replace whole word in a string

void func(string &s, string const& oldVal, string const& newVal) //function to do the replace 
{
    while(s.find(oldVal) != string::npos) //keep finding 
        s.replace(s.find(oldVal), newVal.size(), newVal);
}


int main() //main function, test case
{
    string s("tho");
    func(s, "tho", "though"); 
    cout << s << endl; // expected output: though.
}

Want to replace tho as though, but becoming an infinite loop. Why?

Aucun commentaire:

Enregistrer un commentaire