in C++ A Palindrome is defined as a word, phrase, number, or other sequence of c
ID: 3805347 • Letter: I
Question
in C++
A Palindrome is defined as a word, phrase, number, or other sequence of characters which reads the same backward and forward, Foir this lab, you will write 2 definitions for a Boolean function called palCheck, which returns true if the argument is a palindrome and false if it is not.
One definition will have a string as a argument, and the other will be an interger
The definition for the interger version will be recursive
The definition for the string version can be recursive, but does not have to be.
Your program will ask the user for input, and display the results on the screen
Explanation / Answer
#include <iostream>
using namespace std;
int checkPalRecrsve(int num){
static int sum=0,r;
if(num!=0){
r=num%10;
sum=sum*10+r;
checkPalRecrsve(num/10);
}
return sum;
}
bool palCheck(int num){
if (num==checkPalRecrsve(num))
return true;
return false;
}
bool palCheck(string str){ //function to check string
int len,i,j;
for(len=0;str[len]!='';++len);
for(i=0,j=len-1;i<len/2;++i,--j)
{
if(str[j]!=str[i])
return false;
}
return true;
}
int main()
{
int num ;
char str[50];
cout << "Enter a positive number: ";
cin >> num;
if(palCheck(num)==true)
cout << " The number "<<num<<" is a palindrome ";
else
cout << " The number "<<num<<" is not a palindrome ";
cout<<"Enter a string : ";
cin>>str;
if(palCheck(str)==true)
cout << " The string "<<str<<" is a palindrome ";
else
cout << " The String "<<str<<" is not a palindrome ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.