In: Computer Science
USE C++ for following problem and also use create full program that takes input from user also do not use loops only recursion and try to keep program simple
A palindrome is any word, phrase, or sentence that reads the same forwards or backwards. Here are some palindromes: Level Civic Pot top A man a plan a canal Panama Write a boolean function that determines if a string argument is a palindrome. The function should return true if the argument reads the same forwards and backwards. Your function should ignore spaces and be case-insensitive. Assume the input is just letters and spaces.
NOTE : PROGRAM IS DONE WITH C++ PROGRAMMING LANGUAGE.
IT IS DONE IN DEV-C++ 5.11 SOFTWARE AND IT IS GIVINGE EXACT OUTPUT.
HERE THE PROGRAM IS DONE WITH RECURSION ONLY.
PROGRAM
#include<iostream>
#include<string.h>
using namespace std;
/* define function check_paindrome */
bool check_paindrome(char nm[],int f,int l)
{
/* for first and last character matching */
if(f==l)
{
return true;
}
/* if first and last character do not match each other */
if(nm[f]!=nm[l])
{
/* return false , not palindrome */
return false;
}
/* if index of first character is less than last character plus 1
index */
if(f<l+1)
{
/* recursively call function check_paindrome to check palindrome
*/
return check_paindrome(nm,f+1,l-1);
}
/* if the string scan reaches at end, means it it
palindrome*/
return true;
}
/* palindrome is a function whose return type is boolean
*/
/* if it returns true, means string is palindrome, for false the
string is not palindrome */
bool palindrome(char w[])
{
int p;
p=strlen(w);
/* empty string is also a palindrome */
if(p==0)
{
return true;
}
/* if string is not empty call recursive function check_paindrome
*/
/* if check_paindrome is true, means function palindrome will
return true in main function*/
/* if check_paindrome is false, means function palindrome will
return false in main function*/
return check_paindrome(w,0,p-1);
}
int main()
{
char name[500],st[500];
int i,j=0;
/* console input the string */
cout<<"Enter a string : ";
cin.get(name,500);
/* scan the whole string */
for (i=0; i<strlen(name); i++)
{
/* if i the character of name is
not blank space */
/*function should ignore spaces
*/
if(name[i]!=' ')
{
/* function
should be case-insensitive, so here convert all character in lower
case
and store in
another string st*/
st[j]=tolower(name[i]);
j++;
}
}
/* if palindrome function returns true */
if (palindrome(st)==true)
{
cout<<"The string is palindrome"<<endl;
}
/* if palindrome function returns false */
else
{
cout<<"The string is not palindrome"<<endl;
}
return 0;
}
SCREEN SHOT
OUTPUT