Search code examples
calgorithmpalindrome

C Program to Check for Palindrome String


I wrote two sample programs to check for a palindrome string. But in both I am getting output like, its not a palindrome number. What I am missing?

I strictly assume somehow code is executing my if statement and put flag in to 1. May be because of that length calculation. Anyone has a better idea?

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    int main(void) {
        setbuf(stdout,NULL);
        char name[100];
        int i,length,flag=0,k;
        printf("Enter your name");
        /*scanf("%s",name);*/
        gets(name);
        length=strlen(name);
        for(i=0;i<=length-1;i++)
        {
            for(k=length-1;k>=0;k--)
            {
                if(name[i]!=name[k])
                {
                    flag=1;
                    break;
    
                }
    
                }
            }
    
        if(flag==0)
        {
            printf("Give word is a palindrome");
        }
        if(flag==1)
        {
            printf("This is NOT a palindrome word");
        }
        return 0;
        }

and

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int main(void) {
    setbuf(stdout,NULL);
    char name[100];
    int i,length,flag=0;
    printf("Enter your name");
    /*scanf("%s",name);*/
    gets(name);
    length=strlen(name);
    for(i=0;i<=length/2;i++)
    {
        if(name[i]!=name[length-1])
        {
            flag=1;
        }
    }


    if(flag==0)
    {
        printf("Give word is a palindrome");
    }
    if(flag==1)
    {
        printf("This is NOT a palindrome word");
    }
    return 0;
    }

Solution

  • First Algorithm

    The algorithm you are using in the first program involves comparing each letter to every other letter which does not help in determining if the number is a palindrome and it does not seem fixable.

    Second Algorithm

    The problem with the second approach, however, is you are always comparing name[i] to name[length]. Instead change it to length-i-1. This will start comparing from length-1 and decrement the length of the character by 1 for every next iteration:

    for(i = 0;i <= length / 2;i++)
    {
        if(name[i] != name[length-i-1])
        {
            flag=1;
            break;
        }
    }
    

    gets() and buffer overflow

    Do not use gets. This method is susceptible to a buffer overflow. If you enter a string longer than 100 characters, it will result in undefined behavior. Use fgets instead for deterministic behavior:

    fgets(name, sizeof(name), stdin);
    

    This takes in the size of the buffer and only reads up to sizeof(name) characters.

    Full code

    Ideally, you should consider wrapping the logic to check if the string is a palindrome in a function:

    int is_palindrome(char*);
    
    int main(void) 
    {
        char name[100];
        setbuf(stdout,NULL);
        printf("Enter your name");
        fgets(name, sizeof(name), stdin);
        
        if(is_palindrome(name))
        {
            printf("The given word is a palindrome");
        }
        else
        {
            printf("This is NOT a palindrome word");
        }
        return 0;
        
    }
    
    int is_palindrome(char* name)
    {
        int length = strlen(name);
        int flag = 0, i;
        for(i = 0;i <= length / 2; i++)
        {
            if(name[i]!=name[length-i-1])
            {
                return 0;
            }
        }
        return 1;
    }