C++ Recursions and Palindromes Task 1: As you might know, a palindrome is a stri
ID: 3769174 • Letter: C
Question
C++ Recursions and Palindromes
Task 1: As you might know, a palindrome is a string that reads the same forwards and backwards, ignoring spaces and punctuation. For example, "a man, a plan, a canal, panama" is a well known palindrome. It is simple to recursively express whether a string, x, is a palindrome or not. If x has even length, x is a palindrome if the following conditions hold: x's first and last character is the same the remaining characters of x are a palindrome. Formally, the above can be expressed as the following recurrence relation where x has length n: palindrome(x) = (x[theta]==x[n-l]) AND palindrome(x[1..n-2]) Write a program that inputs a string from the user and prints a message indicating whether the string is a palindrome or not. The program should do the following: Input a string from the user Filter out spaces and punctuation. You may wish to consult an ASCII table to figure out how to do this google finds many such tables. Check that the resulting string has even length. Output an error message and repeat the above otherwise. Call a recursive function to check whether the string is a palindrome Print an appropriate message to the user Task 2: Generalize the previous program to also handle odd length strings. You will need to generalize the previous recurrence relation. Task 3: In the above task, you filtered spaces and punctuation in the main program. Modify the solution to instead have the recursive function itself ignore spaces and punctuation. In other words, the work in step 2 should now I part of step 3.Explanation / Answer
Hi ,implemented code according to your Requirements..
CODE:
#include<iostream>
using namespace std;
int main(){
char str1[20];
int i, lnth;
int flg = 0;
cout << "Enter a string: ";
cin >> str1;
lnth = strlen(str1);
for(i=0;i < lnth ;i++){
if(str1[i] != str1[lnth-i-1]){
flg = 1;
break;
}
}
if (flg) {
cout << str1 << " is not a palindrome" << endl;
}
else {
cout << str1 << " is a palindrome" << endl;
}
system("pause");
return 0;
}
Sample Output:
Enter a string:rar
rar is a palindrome.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.