Testing to see if a word is a palindrome. Using pointers and not arrays?? My cod
ID: 3562407 • Letter: T
Question
Testing to see if a word is a palindrome. Using pointers and not arrays??
My code is:
bool testPalindrome(char *str)
{
char *front = str;
char *rear = str-strlen(str)-1;
str = str + strlen(str) - 1;
for (unsigned i = 0; i<strlen(str); i++)
{
if (*front++ != *rear--)
{
cout << "Not a Palindrome." << endl;
return false;
}
else
{
cout << "It's a Palindrome." << endl;
return true;
}
}
return false;
}
It is saying everything is not a palindrome even if it is.
what is wrong?
and thanks for the help.
Explanation / Answer
/*Please find the following Solution
If you find its helpful please rate.
If you have any doubts please comment*/
#include<iostream>
using namespace std;
bool testPalindrome(char *str);
int main()
{
char str[30]="hao";
return 0;
}
bool testPalindrome(char *str)
{
char *front = str;
char *rear = str-strlen(str)-1;
str = str + strlen(str) - 1;
for (unsigned i = 0; i<strlen(str); i++)
{
if (*front++ != *rear--)
{
cout << "Not a Palindrome." << endl;
return false;
}
else
{
cout << "It's a Palindrome." << endl;
return true;
}
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.