my code below return random values matching starting index 2 strings. example, if s = mississippi, , t = sip, function should return 6. set code up, print out value of m before returns it. indeed, program prints out 6, returns random values in millions.
int index_of_sub(string s, string t, int m) { if(s.length() - m < t.length()) { return -1; } if(s.substr(m, t.length()) == t) { cout << m << endl; return m; } else { index_of_sub(s, t, m + 1); } }//end index_of_sub function int index_of(string s, string t) { return index_of_sub(s, t, 0); }//end index_of function
you need put return statement on last else.
else { return index_of_sub(s, t, m + 1); ^^^^^^ } since not explicitly returning result of recursive call, garbage value being returned why getting such strange results.
Comments
Post a Comment