C Program to Check a String is Palindrome

Here is the C program to check string is palindrome or not. A string is palindrome, if string remains same after reversing it's character. For example, "madam" is a palindrome string whereas apple is not a palindrome string.

To check whether a string is palindrome or not, we first make a copy of string and then reverse it. We compare original string and it's reverse, if both are equal than it is a palindrome string otherwise not a palindrome.


C program to check a string is palindrome using strrev function

In this program, we use strcpy, strrev and strcmp standard library functions of string.h to copy, reverse and compare strings respectively.

We first take an input string from user using scanf and store it in an character array. Then we make a copy of input string using strcpy and reverse it using strrev function. Using strcmp function we compare input string and it's reverse, If both are equal than input string is palindrome otherwise not a palindrome.

#include <stdio.h>
#include <string.h>
 
int main() {
   char inputArray[100], reversedArray[100];

   printf("Enter the string\n");
   scanf("%s", inputArray);
   
   /* Copy input string and reverse it*/
   strcpy(reversedArray, inputArray);
   
   /* reverse string */
   strrev(reversedArray);
   
   /* Compare reversed string with inpit string */
   if(strcmp(inputArray, reversedArray) == 0 )
      printf("%s is a palindrome.\n", inputArray);
   else
      printf("%s is not a palindrome.\n", inputArray);
      
   return 0;
}
Output
Enter the string
MADAM
MADAM is a palindrome.
Enter the string for palindrome check
TechCrashCourse
TechCrashCourse is not a palindrome.

C program for palindrome check without using library functions

In this program, we use the fact that, If a string is palindrome then leftmost character of the string is equal to the rightmost character of the string. We initialize two integer variables to point to first and last characters of string. Inside while loop we compare left and right characters, if mismatch found then not a palindrome otherwise palindrome string.

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

int main(){
    char inputString[100];
    int leftIndex, rightIndex, length = 0;
    
    printf("Enter a string\n");
    scanf("%s", inputString);
    
    /* Find length of input string */
    while(inputString[length] != '\0')
        length++;
        
    if(length < 1) 
        return 1;
        
    leftIndex = 0;
    rightIndex = length -1;

    while(leftIndex < rightIndex){
        if(inputString[leftIndex] != inputString[rightIndex]){
            printf("%s is not a Palindrome \n", inputString);
            return 0;
        }
        leftIndex++;
        rightIndex--;
    }
    
    printf("%s is a Palindrome \n", inputString);

    return 0;
}
Output
Enter a string
asdfdsa
asdfdsa is a Palindrome
Enter a string
qwerty
qwerty is not a Palindrome

C program for palindrome check using recursion


We can check whether a string is palindrome or not using recursion by breaking this problem into a smaller problem. Let isPalindrome be a function that takes a string, left_Index and right_Index as input and checks whether input string is palindrome or not. Using this function, here is the recursive equation for palindrome check

isPalindrome(string, i, j) = swap(string, i, j) + isPalindrome(string, i+1, j-1)
Here is the recursive algorithm and C program for palindrome check : Palindrome check using recursion


Related Topics
C program to check a number is palindrome or not
C program to convert string to integer
C program to remove vowels from a string
C program to copy a string
C Program to find frequency of characters in a string
C program to find a substring from a given string
C program to reverse a string
List of all C programs