Boolean/ For-Loop Problem

Hi I am trying to write a program that will determine if a set of characters is a Palindrome or not. I can get it to run, but everything I enter is determined a Palindrome. How do I fix it so that the boolean value changes? Below is the 'for-loop' that should be changing the value, but isn't. Thanks

for (int i = 0; i < length/2; i++)
{
if (s[i] == s[length-1])
{}

else
{
isPal = false;
i = length/2;
}
}


Last edited on
You have if( isPal = true ) which both assigns a one to isPal and tests to see that it is non-zero. You need to change it to if( isPal == true )
I see no error in that code snippet, so the error is somewhere else. Post your full code.
Also:
i = length/2;
Don't do this! This is what break exists for.

And it should be just if (isPal)
Last edited on
Wow thanks guys! You guys are awesome.

Could you explain what a break is? I have never heard of one? Thanks
closed account (Gz64jE8b)
break;

It does what it says on the tin, breaks the loop.
break; immediately jumps out of the loop. See here:
http://www.cplusplus.com/doc/tutorial/control/#break
Ahh Thanks!

ok. So I ran it..and it will identify words/characters less than 3 characters but it won't pick up on "noon" or "hannah"....it calls those non palindromes...ugh! help! any pointers? Thanks!
if (s[i] == s[length-1])

You're comparing each character to the very last character in the string. So your code will think "nnnnooon" is a palindrome because all character before the halfway point match the last 'n'.
It does...but how do you fix that?
I got it. Thanks everyone! you are all awesome!

For anyone reading this in the future: the answer to the above stated problem was changing the 'if' statement to s[i] == s[length-1-i]

catch ya'll later
Topic archived. No new replies allowed.