tags:

views:

144

answers:

4

I made a program to find if a entered string is palindrome or not palindrome but it always says that its not a palindrome

#include <conio.h> 
#include <graphics.h> 
#include <string.h>
void main(void)
{
    int i,len,halflen,flag=1;
    char str[50];
    clrscr();
    printf("Enter a string:\n");
    gets(str);
    len=strlen(str);
    halflen=len/2;
    for(i=0;i<halflen;i++)
    {
        if(str[i]!=str[i+halflen])
            flag=0;
        break;

    }
    if(flag)
        printf("It is a Palindrome.");
    else
        printf("It is not a Palindrome.");
    getch();
}
+3  A: 

To give you a clue I've done some tidier indenting of a bit of your code:

for(i=0;i<halflen;i++)
    {
        if(str[i]!=str[i+halflen])
            flag=0;
        break;
    }
Will
And this is why the grey-beards use braces everywhere, even when some people might think they don't need them.
Will Dean
@Will Dean: Word.
Paul Nathan
This is a problem, but not the whole problem
Ned Batchelder
+7  A: 

Your central comparison is flawed:

if (str[i] != str[i+halflen]) 

This isn't comparing the two characters you think it is.

Try entering "HelloHello" into your program, it will say it is a palindrome!

You need to compare these two:

if (str[i] != str[len-i-1])

(and fix the braces, as suggested in the other answer)

Ned Batchelder
That should be `if (str[i] != str[len-1-i])`, If you do not add the -1 it will compare the first character to the '\0' at the end and will fail for all but empty strings.
David Rodríguez - dribeas
Should be if (str[i] != str[len-1-i]), right?
Andrew Flanagan
wait a second ! does strlen count the \0 too ? :S
fahad
@fahad, no. `strlen` gives the number of chars before the `\0`.
Matthew Flaschen
does strlen starts counting from 0 or 1?..
fahad
@fahad, the problem is that `"Hello"` is `['H','e','l','l','o',0]` and has length 5, as expected. If you start comparing element `0` with element `len-0` you will compare the `'H'` with the 6th element (at position 5), which is `'\0'`.
David Rodríguez - dribeas
Yes all, sorry, it should be str[len-i-1]
Ned Batchelder
+1  A: 

From the 2005 version of myself:

bool isAlphaNumeric(char c)
{
    return (iswalpha(c) || iswdigit(c));
}

bool isPalindrome(char *str)
{
    /* A man, a plan, Anal Panama!!! */
    if(*str == '\0')
    {
        return false;
    }

    int len = strlen(str);
    if(len <= 1) return true;

    char *start = str;
    char *end = start + len - 1;

    while(start < end)
    {
        if(!isAlphaNumeric(*start))
        {
            *start++;
            continue;
        }
        if(!isAlphaNumeric(*end))
        {
            *end--;
            continue;
        }
        if(towlower(*start) != towlower(*end))
        {
            return false;
        }
        *start++;
        *end--;
    }
    return true;
}
Chris Doggett
There are a few stray `*` in the solution that make it less readable than it should... `*start++`, `*end--` should be `++start` and `--end` (the extra dereference can be easily elided by the compiler knowing that it is not used, but for us humans it only adds to confusion --or that´s my belief. -- I have corrected the `"` => `'\0'`, as that was surely a copy error.
David Rodríguez - dribeas
Never use the character-testing functions like `iswalpha` or `towlower` with plain `char` as the argument. It leads to *undefined behavior*. Better use `isalnum((unsigned char)c)`. And why do you use `iswalpha` at all? That function is for wide characters, and you just pass it a simple character.
Roland Illig
Thanks for the suggestions. This was from 5 years ago, which was 4 years after the last time I'd regularly used C/C++. Thanks for fixing the errors, too. I copied it from my blog, and Wordpress screwed up a lot of the code with its automatic formatting, turning "--" into a long dash, and removing a few other characters. Thought I'd caught them all.
Chris Doggett
+2  A: 

Here you want something like...

    if(str[i]!=str[strlen (str) - i - 1])
    {
        flag = 0;
        break;
    }

The break needs to go in the if block otherwise it will always get executed. Initialising flag at some point would be a good idea, too. If I might be permitted an observation, ALWAYS enclose the if-block and else block in curly brackets even if there is only one statement; it would save you several of the problems you've got here.

Later - edited per Mr Rodriguez' comments below.

Brian Hooper
Again the problem of the off-by-one. You must offset the right term with a -1 for this to work.
David Rodríguez - dribeas
You're right. Closing the brackets correctly would have been good as well.
Brian Hooper