Write a program using string functions that determine if the
input word is a palindrome. A palindrome word is a word
that produces the same word when it is reversed.
Sample Input/Output Dialogue:
Enter a word: CUTE
Reversed: ETUC
“Nice”
Enter a word: MAD
Reversed: DAM
“Nice”
#include <bits/stdc++.h>
using namespace std;
bool isPalRec(char str[], int s, int e)
{
if (s == e)
return true;
if (str[s] != str[e])
return false;
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
bool isPalindrome(char str[])
{
int n = strlen(str);
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
int main()
{
char str[100];
cout<<"Enter a string ";
cin.getline(str,100);
if (isPalindrome(str))
cout << "Yes, given string is a palindrome";
else
cout << "No, given string is not a palindrome";
return 0;
}
Comments
Leave a comment