C CODING A palindrome is a word phrase number or other seque
C++ CODING!!!
A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward or forward. Write a function isPalindrome, which takes a string value as parameter to check if the given string is a palindrome. The return value should a Boolean type.
You should also write a main function to prompt the user to enter a string, and call isPalindrome to check if the input string is palindrome. Based on the return value of isPalindrome, print out either “The input is a palindrome string” or “The input is NOT a palindrome string”. You can assume the input string is lowercase.
Note that if str is a variable of type string, then str.at(i) return s the character at the ith position. The position of the first character is 0. Also, str.length() returns the length of the str, that is, the number of characters in str.
E.g.,
Input string: 1234567890987654321
Output: The input is a palindrome string.
Input string: abcdefedca
Output: The input is NOT a palindrome string.
Grading scheme: Define isPalindrome function with string type parameter and boolean return type - 5 points; Check if the given string is a palindrome - 10 points; Write a main function to call isPalindrome – 5 points.
Solution
#include <iostream>
#include<cstring>
using namespace std;
bool isPalindrome(int start, int end, string &str)
{
if (start >= end) // all characters in string are checked
return true;
if (toupper(str[start]) != toupper(str[end])) //compare 1st character with last,2nd with second last and so on after capitalizing
return false;
return isPalindrome(++start, --end, str); // increment start index and decrement end index
}
int main()
{
string str;
int length;
cout<<\"Enter the String to check:\";
cin>>str;
length = str.length();
if(isPalindrome(0,length-1,str))
cout<<\"\ \"<<str<<\" is a palindrome\";
else
cout<<\"\ \"<<str<<\" is not a palindrome\";
return 0;
}
output:
