C Program to check whether a given string is palindrome or not

       A palindrome is a word, phrase, number, or other sequences of characters which reads the same backward as forward, such as madam or racecar. This program finds whether given string is palindrome or not by checking the first and last characters of a given string until for loop reaches the middle of the string. So whenever there is a mismatch of characters, it indicates that given string is not a palindrome otherwise given string is a palindrome.

C Program to check whether a given string is palindrome or not



Source Code to find palindrome or not:

#include<stdio.h>
#include<tstring.h>
int main()
{
    int n,i,j,chk=1;
    char s[30];
    printf("\n Enter the string:");
    scanf("%[^\n]",s);                           // Step 1
    n=strlen(s)-1;
    for(i=0,j=n; i < n/2; i++,j--)               // Step 2 
        if(s[i]!=s[j])                           // Step 2(i)
        {
            chk=0;
            break;
        }
    if(chk==1)
        printf("String is Palindrome");               // Step 3
    else
        printf("String is not Palindrome");
    return 0;
}


edit-code-image

Sample Test cases :

1.  Enter the string: abccba
    String is Palindrome

2. Enter the string: abdcba
   String is not Palindrome

3. Enter the string: abcddcba
   String is Palindrome

Explanation:

Step 1 : The string is read from the user using scanf() function.

Step 2 : The start and end characters of the string are checked upto half the length of the string using for loop.

Step 2(i): If any of the characters are not equal then it implies that given string is not palindrome then we exit from the loop and prints output on the screen.

Step 3 : The result is printed on the output screen using printf() function.

More Insights:

1. Palindrome

1. Palindrome examples

More Content:Explore More