216k views
3 votes
C++A palindrome is a string such as "madam", "radar", "Dad", and "I", that reads the same forwards and backwards. The empty string is regarded as a palindrome. Wrtie a recursive functionbool isPalindrome(string str, int lower, int upper)that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is a palindrome. Test your function by writting a main function that repeatedly asks the user to enter strings terminated by the ENTER key. These strings are then tested for palindromecity. The program termintaes when the user presses the Enter key without typing any haracters before it.

User Stink
by
4.2k points

1 Answer

7 votes

Answer:

See explaination

Step-by-step explanation:

#include <iostream>

#include<string.h>

using namespace std;

bool isPalindrome(string str, int lower, int upper){

if(str.length() == 0 || lower>=upper){

return true;

}

else{

if(str.at(lower) == str.at(upper)){

return isPalindrome(str,lower+1,upper-1);

}

else{

return false;

}

}

}

int main(){

string input;

cout<<"Enter string: ";

cin>>input;

if(isPalindrome(input,0,input.length()-1)){

cout<<input<<" is a palindrome"<<endl;

}

else{

cout<<input<<" is NOT a palindrome"<<endl;

}

return 0;

}

User Viswesvar Sekar
by
5.1k points