I am trying to replace "the" with "that". My original sentence is "the ball is under the car". I can successfully change the first "the". But when I try to change the second "the" string1 ends up being "that ball us under that".So it cuts off the world "car". What am I doing wrong? Any help will be greatly appreciated.
#include <iostream>
#include <string>
using namespace std;
void replaceSubstring(string, string, string);
int main() {//start main
string string1 = "the ball is under the car ";
string string2 = "the";
string string3 = "that";
int x;
cout << "The orginal strings consist of: " << endl;
cout << "String1 = " << string1 << endl;
cout << "String2 = " << string2 << endl;
cout << "String3 = " << string3 << endl;
replaceSubstring(string1,string2, string3);
cin >> x;
}//end main
void replaceSubstring(string string1, string string2, string string3) {
int spot1;
int spot2;
spot1 = string1.find(string2, 0);
spot2= string1.find(string2, 3);
string1.replace(spot1, (spot1 + 3), string3);
cout << string1 << endl;
string1.replace(spot2, (spot2 + 3), string3);
cout << string1;
}