You never give any value to your strings in main so they are empty, and thus obviously the function returns an empty string.
Replace:
string str1, str2, str3;
with:
string str1 = "the dog jumped over the fence"; string str2 = "the"; string str3 = "that";
Also, you have several problems in your replaceSubstring function:
int index = s1.find(s2, 0); s1.replace(index, s2.length(), s3);
std::string::findreturns astd::string::size_type(aka.size_t) not anint. Two differences:size_tis unsigned, and it’s not necessarily the same size as anintdepending on your platform (eg. on 64 bits Linux or Windowssize_tis unsigned 64 bits whileintis signed 32 bits).- What happens if
s2is not part ofs1? I’ll leave it up to you to find how to fix that. Hint:std::string::npos😉