Question
I have this palindrome c++ function, it works with racecar and ana, but fails with avid diva. Maybe because of the whitespace in between the
I have this palindrome c++ function, it works with racecar and ana, but fails with avid diva. Maybe because of the whitespace in between the words. How do i fix it so that it works including on words with a space in between like" avid diva" ,we can not use the cstring library or any standard libraries? Below is my code
#include
int main(){ char string1[20]; int i, length; int flag = 0; cout << "Enter a string: "; cin >> string1; //gets char length int j = 0; while (string1[j] != '\0') { ++j; } length = j;
// palindrome function for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; }}
if (flag) { cout << string1 << " is not a palindrome" << endl; } else { cout << string1 << " is a palindrome" << endl; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started