Does the conditional statement look right at all? |
Yes, a bit, but it seems over-complicated - but if it's working with the rest of your program then that's fine.
Imagine you've got a 3-letter palindrome: 'dad'
-both pointers are at 'd', letters match
-increment front, decrement back
-pointers are equal
It looks like your function should handle that OK.
Four-letter palindrome: 'abba'
-both pointers at 'a', letters match
-increment front, decrement back
-both pointers at 'b', letters match
-increment front, decrement back
Now back pointer's at the first 'b', front pointer's at the 2nd 'b'.
Couldn't you test for
head < tail
?
1 2 3 4
|
while ( head != tail && head < tail )
{
// ...
} // while
|
Actually I've only just noticed (duh!) there's a logic error in your code as well - I think those logical OR's (
||
) should've been AND's (
&&
). Otherwise if only one of those conditions holds, your loop will continue. That's not what you want - you want to pull out of the loop as soon as one of those conditions is met.
Hope that helps - post some more code up if you're still stuck ;)