Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string text, search, replacement, res;
getline(cin,text); getline(cin,search); getline(cin,replacement);
for (string::size_type pos = 0; pos < text.size(); pos++) {
if (text.find(search,pos) == pos) {
res += replacement;
pos += search.size()-1;
} else {
res += text[pos];
}
}
cout << res << "\n";
return 0;
}
I'm not sure if this is what you are looking for, but it may give you a better idea to how to approach the problem. What the code does is finds any matches from left to right, if it finds one - it replaces it and increments the counter to skip through the old text. Otherwise it just appends the current character. It will replace more than one, so if you had "hello world world" and your search is "world" and replacement is "hello" then you would get "hello hello hello". There are more efficient ways of doing this because you do some duplicate searches as well as unnecessary searches (i.e. searches whole string from current position every time). As an exercise you can modify it to use the substr() function and do a comparison to emulate the "find" which should make it faster.