I have a code to replace the content of a sub-string of a given string. It does not work as I expected.
From my understanding, s3.find("they") would return 6. Since pos is not the same as string::npos, then, from position 6, 2 characters in s3 are replaced by string s4. So, s3 would be "There boey go again!" after the replacement. Yet, the output of s3 is, "There Bob and Bill go again!". Could anyone help to explain?
#include <iostream>
#include <string>
using namespace std;
string prompt("Enter a line of text: "),
line( 50, '*');
int main()
{
string s3("There they go again!"),
s4("Bob and Bill");
int pos = s3.find("they");
if( pos != string::npos )
s3.replace(pos, 2, s4);
cout << s3 << endl;
cout << s4 << endl;
return 0;
}