-
Recursive problem...
A palindrome is a word that is spelled exactly the same backwards as forwards(e.g level)write a recursive function that returns true if the word given to it is a palindrome
#include <iostream>
#include <string>
using namespace std;
void is_palindrome(string s);
int main()
{
string str;
cout<<"Please enter a string : "<<endl;
cin>>str;
is_palindrome(str);
return 0;
}
void is_palindrome(string s)
{
if (s.length()>0)
{
is_palindrome(s.substr(1,s.length()-1));
cout<<s[0];
}
}
i just can use the recursive to cout a backwards of string
but i don't know how to save it to a variable and compare it..
Pls help.... Thx...
-
What is the code for substr()? I might be able to help if I can see this.
-
2 comments:
1. There is much more efficient code you can write without recursion. Only do so if your teacher told you to.
2. Search the board if you don't have to. There are a billion posts on this.