For computer science class, we are getting into recursive functions, which I personally don't like, but we need to use it. The following code is intended to check if the string input 's' is palindrome.
bool palindrome(string s)
{
int len = s.length();
char start = s.at(0);
char last = s.at(0);
if(len>1)
{
last = s.at(len);
}
if(start == last && len<=2)
{
return true;
}
else if(start != last)
{
return false;
}
else
{
s = s.substr(1, s.size() - 2);
return palindrome(s);
}
}
After running this, and inputting a string, I get a runtime error 'std::out_of_range', and I'm not sure where I'm screwing up. I'm very new to recursive functions and c++ in general, so any help would be fantastic.
If any additional info is needed which I didn't provide please tell me.
*NOTE: We need to use a bool function and can only call in the string which we input into the function.
last = s.at(len);
should be
last = s.at(len-1);