I participate in programming in the first C ++ class, and recently we were given a task to implement a recursive program that finds the first occurrence of a given substring in a given string.
For instance:
int StringIndex("Mississippi", "sip");
The hint we give is to use a recursive helper function that takes an index as a parameter.
Here is what I have done so far:
int index_of(string s, string t)
{
int index = 0;
if (s[index] == NULL)
return -1;
else if (starts_with(s, t, ++index))
{
return index;
}
return index_of(s, t);
}
bool starts_with(string s, string t, int index)
{
if (t[index] != s[index] || s[index] == NULL)
return false;
return starts_with(s, t, ++index);
}
I get an error and I don’t understand why. So could someone help me understand why I am getting this error? And help me sharpen in the right direction, how to fix it?
source
share