May I request that you put your code inside the tags, [co
de] your code here [/co
de] - use the
<>
button in the format menu on the right.
Also, the latest code you posted is incomplete, it has a return statement and closing brace but no matching code at the beginning, which makes it hard to follow.
I think you have not quite understood the code correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int palindrome (char * word)
{
int pal=1;
char *back, *forward;
int size=strlen(word);
back = word+size-1;
forward=word;
while ( back>forward)
{
if (*forward != *back)
pal=0;
forward++;
back--;
}
return pal;
}
|
There are two pointers,
char *back, *forward;
forward
is set to point to the first letter of the word.
back
is set to point to the last letter of the word.
The two characters pointed to are compared. If they are not the same, the word is not a palindrome, and so pal is set to zero.
Then the
forward
pointer is moved forward by one character, the
back
pointer is moved backward by one character. When the two pointers meet (or cross over) in the middle, the loop ends. Otherwise, it continues to compare each pair of characters.
The word itself remains unchanged at all times, it does not get longer or shorter, or change in any way.