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?
When writing recursive functions you should always keep two things in mind: you need stop conditions which end the recursion and you have to get closer to a stop condition with each function call. If you fail to check stop conditions or if your function doesn’t get closer to a stop condition during each call, you’ll run into stack overflows (or infinite loops).
The problem in your first function is that it doesn’t get closer to the stop condition. At the end you “return index_of(s, t)” without modifying s or t in the meantime. So the function will start over with the same parameters, again and again until you run into a stack overflow.
Another problem is in your starts_with function. It only returns false but never true.