But if you want to do word replacement, rather than (dumb) string replacement, you'll need to look for whole words and then handle them.
1 2 3 4 5 6 7
string str = "This is a lonely phrase.\n\
It needs another one for company!";
string str1 = "one";
string str2 = "eight";
str.replace( str.find(str1), str1.length(), str2);
cout << str;
This is a leightly phrase.
It needs another one for company!
Of course, that doesn't work if you want to replace a word that is first or last on a line, or is part of a hyphenated phrase, or occurs in quotes.
I'm not suggesting you implement a version that does. More of a warning to the OP about using the code as-is. More than enough code has been contributed for him to figure out what needs to be done, I'm sure.