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::find
returns astd::string::size_type
(aka.size_t
) not anint
. Two differences:size_t
is unsigned, and it’s not necessarily the same size as anint
depending on your platform (eg. on 64 bits Linux or Windowssize_t
is unsigned 64 bits whileint
is signed 32 bits).- What happens if
s2
is not part ofs1
? I’ll leave it up to you to find how to fix that. Hint:std::string::npos
😉