Hello,
I'm a programming student in my first C++ class, and recently we were given an assignment to implement a recursive program that finds the first occurrence of a given substring in a given string.
For example:
int StringIndex("Mississippi", "sip"); // this would return 6
The hint we are given is to use a recursive helper function that takes the index as a parameter.
Here is what I've 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 am getting a stack overflow error, and I don't understand why. So would somebody mind helping me understand why I'm getting this error? And help me get pointed in the right direction as to how to fix it?
Thanks!