Write a recursive function named isPalindrome that takes a C++ string as a parameter and returns a bool indicating whether that string is exactly the same forwards and backwards. For example, “racecar” would return true, but “Racecar” would return false. The file must be named: isPalindrome.cpp.
Expert Answer
#include <iostream>
#include <string.h>
using namespace std;
bool chkpallen(char[]);
bool pallen(char[],int ,int);
int main()
{
char name[]=”madam”;
if (chkpallen(name))
cout<<“it is pallendrome”<<endl;
else
cout<<“it is not pallendrome”<<endl;
return 0;
}
bool chkpallen(char name[])
{
int len=strlen(name);
if (len==0)
return true;
return pallen(name,0,len-1);
}
bool pallen(char name[], int a, int b)
{
if (a==b)
return true;
if (name[a]!=name[b])
return false;
if (a<b+1)
return pallen(name,a+1,b-1);
return true;
}
output:screenshot:
output when it is not pallendrome string:
#include <iostream>
#include <string.h>
using namespace std;
bool chkpallen(char[]);
bool pallen(char[],int ,int);
int main()
{
char name[]=”racecars”;
if (chkpallen(name))
cout<<“it is pallendrome”<<endl;
else
cout<<“it is not pallendrome”<<endl;
return 0;
}
bool chkpallen(char name[])
{
int len=strlen(name);
if (len==0)
return true;
return pallen(name,0,len-1);
}
bool pallen(char name[], int a, int b)
{
if (a==b)
return true;
if (name[a]!=name[b])
return false;
if (a<b+1)
return pallen(name,a+1,b-1);
return true;
}
/*comments
here it is recurisive function to calculate pallendrome of given string
with one main function and 2 userdefined functions
*/