Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Based on the code below, I'm supposed to Write a C++ program that determines if a given string is a palindrome. When I enter in

Based on the code below, I'm supposed to Write a C++ program that determines if a given string is a palindrome. When I enter in a string, it's supposed to properly tell me if it's a palindrome or not. The problem is that no matter what I enter, it tells me what I enter isn't a palindrome. I need help correcting this error in the code below.

#include #include using namespace std; //function prptotypes eclared void transform( char *raw, char *testStr); bool testPalindrome(char *str); int main() { //char arrays declared char str[80]; char convertedStr[80]; //reading string from user cout<<"Enter String: "; cin >> str; //calling functions to remove unwanted characters transform( str, convertedStr); //calling function to check palindrome or not bool res = testPalindrome(convertedStr); if(res){ cout<<" Entered String is Palindrome"; } else{ cout<<" Entered String is not a Palindrome"; } return 0; } //this function will convert all letters to upper case and remove unwanteed extra caracters void transform( char *raw, char *testStr){ int strlength = strlen(raw); int front = 0; for(int i = 0; i < strlength; i++) { raw[i] = toupper(raw[i]); //converting to upper case } for(int i = 0; i < strlength; i++) { if(isalpha(raw[i])){ //checking for aonly alphabets testStr[front] = raw[i]; front++; } } } //palindrome checking function bool testPalindrome(char *str){ bool result = true; int strlength = strlen(str); //getting string length for(int i=0, j = strlength-1; i< strlength/2;i++,j--){ //palindrome checking if(str[i]==str[j]){ //checking from front and back result; break; } else { result = false; } } return result; //returning result }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions