C program to check Palindrome

Write a C program to check whether a String / Number is Palindrom or not.

#include<stdio.h>
int main()
{
    char a[50];
    int l,i=0,j,flag=1;
    printf("Enter A String: ");
    gets(a);
    for(l=0;a[l]!='\0';l++);
     //to determine length of String
    j=l-1; // l is the length of String
    for(i=0;i<l/2;i++)
    {
        if(a[i]!=a[j])
            flag=0;
        j--;
    }
    if(flag==1)
        printf("Palindrome");
    else
        printf("Not Palindrome");
    return 0;
}


Output: Enter A String: madam ↵
               Palindrome

/*Explaination*/
/*  First we are counting no of characters. The value length will be assigned to l. Then we start comparing 1st character with last character. 2nd character with 2nd last element and so on till n/2th position(half of string). Initially flag was set as 1 but if previous condition fails then it will change the value of flag to 0. Lastly if flag is 0 i.e., the matching condition doesn't satisfied then It will print "Not Palindrome"*/

Post a Comment

0 Comments