Im working a a program that uses pointers to check if a word is a palindrome or not. I know the code needs to have the pointers go through the array one starting at the beginning and one at then end comparing the chars to see if they are equivalent. I'm wondering where I need to go from here?
#include <iostream>
usingnamespace std;
bool isPalindrome(constchar *);
int main()
{
char x[100];
cout << "Enter a word or phrase: ";
cin.getline(x,100);
if(isPalindrome(x))
cout << "This is a palindrome.\n";
else
cout << "This is NOT a palindrome.\n";
return 0;
}
bool isPalindrome(constchar * xPtr)
{
bool result = true;
constchar * endPtr = xPtr;
// loop to move endPtr to the NULL character in the string
for ( ; *endPtr != NULL; endPtr++)
{
}
// move endPtr to the character before the NULL
endPtr--;
// loop to check for not a palindrome
// each time in the loop, increment xPtr and decrement endPtr
for ( ; *xPtr != *endPtr; *xPtr++, *endPtr--)
return result;
}
I know the code needs to have the pointers go through the array one starting at the beginning and one at then end comparing the chars to see if they are equivalent.
Not really. You can think of the string as an array and index into that array using integers.