c++ - Why does my "string mingling" method return unexpected results? -
i'm trying implement simple "string mingling" method, recursively mingles 2 strings of equal size (e.g. cat , dog becomes cdaotg -- first letter string 1, first letter string 2, , on).
my method follows:
string minglestrings(string s1, string s2, int index) { if (index >= s1.length()) { return ""; } else { string mingled = ""; mingled += s1[index] + s2[index]; mingled += minglestrings(s1,s2,++index); return mingled; } }
when use subscript operator on string (s1[index]), returns entire string index. specific character of string @ index, need type s1[index,index]. new me.
the problem code on line:
mingled += s1[index] + s2[index];
what adding codes of characters @ index
, , appending result of addition string single character.
it should 2 separate operations:
mingled += s1[index]; mingled += s2[index];
this way append single character string each time call +=
, producing result expect.
Comments
Post a Comment