palindrome is a string that reads the same both forward and
palindrome is a string that reads the same both forward and backward.
C++
For example, the string \"madam\" is a palindrome.
Write a program that uses a recursive function to check whether a string is a palindrome.
Your program must contain a value-returning recursive function that returns true if the string is a palindrome and false otherwise. Do not use any global variables; use the appropriate parameters.
Solution
Solution.cpp
#include <iostream>//header file for input output function
#include <cstring>//header file for string functions
using namespace std;//it tells the compiler to link std namespace
bool isPalindrome(char *inputString, int leftIndex, int rightIndex);//function declaration
int main(){//main function
char Str[100];
cout<<\"Enter a string for palindrome check\ \";
cin>> Str;//keyboard inputting
//calling function
if(isPalindrome(Str, 0, strlen(Str) - 1)){
cout<<Str<<\" is a Palindrome \ \"<< Str;
} else {
cout<<Str<<\" is not a Palindrome \ \"<< Str;
}
return 0;
}
/*
* Function to check whether a string is palindrome or not
*/
bool isPalindrome(char *inputString, int leftIndex, int rightIndex){
/* Input Validation */
if(NULL == inputString || leftIndex < 0 || rightIndex < 0){
cout<<\"Invalid Input\";
return false;
}
/* Recursion termination condition */
if(leftIndex >= rightIndex)
return true;
if(inputString[leftIndex] == inputString[rightIndex]){
return isPalindrome(inputString, leftIndex + 1,rightIndex - 1);
}
return false;
}
output
madam
madam is a Palindrome
