C Program to Check a String is Palindrome

  • Write a C program to check string is palindrome or not.
  • C program to check palindrome string.

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.

/*
* C Program to check given string is palindrome or not
*/
#include <stdio.h>
#include <string.h>
 
int main()
{
   char inputArray[100], reversedArray[100];

   printf("Enter the string for palindrome check \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);
      
   getch();
   return 0;
}
Program Output
Enter the string for palindrome check
MADAM
MADAM is a palindrome.
Enter the string for palindrome check
TechCrashCourse
TechCrashCourse is not a palindrome.

C program for palindrome check without using string 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.

/*
* C Program to check given string is pallindrome or not
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main(){
    char inputString[100];
    int leftIndex, rightIndex, length = 0;
    printf("Enter a string for palindrome check\n");
    scanf("%s", inputString);
    /* Find length of input string */
    while(inputString[length] != '\0')
        length++;
    /* If length of string is less than 1, ERROR */
    if(length < 1) 
        return 1;
        
    /* Initialize leftIndex and rightIndex to first and 
     last character of input string */
    leftIndex = 0;
    rightIndex = length -1;
    /* Compare left and right characters, If equal then 
     continue otherwise not a palindrome */
    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);
    getch();
    return 0;
}
Program Output
Enter a string for palindrome check
asdfdsa
asdfdsa is a Palindrome
Enter a string for palindrome check
qwerty
qwerty is not a Palindrome

C program for palindrome 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