Open In App

C Program to Check for Palindrome String

Last Updated : 10 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

A string is said to be palindrome if the reverse of the string is the same as the string. In this article, we will learn how to check whether the given string is palindrome or not using C program.

The simplest method to check for palindrome string is to reverse the given string and store it in a temporary array and then compare both of them using strcmp() function. If they are equal, then the string is palindrome, otherwise, it is not. Let’s look at its implementation.

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

char *strrev(char *str) {
    int len = strlen(str);
  
      // Temporary char array to store the
    // reversed string
    char *rev = (char *)malloc
      (sizeof(char) * (len + 1));
  
    // Reversing the string
    for (int i = 0; i < len; i++) {
        rev[i] = str[len - i - 1];
    }
    rev[len] = '\0';
    return rev;
}

void isPalindrome(char *str) {

    // Reversing the string
    char *rev = strrev(str);

    // Check if the original and reversed
      // strings are equal
    if (strcmp(str, rev) == 0)
        printf("\"%s\" is palindrome.\n",
               str);
    else
        printf("\"%s\" is not palindrome.\n",
               str);
}

int main() {
    
      // Cheking for palindrome strings
    isPalindrome("madam");
      isPalindrome("hello");

    return 0;
}

Output
"madam" is palindrome.
"hello" is not palindrome.

Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n), for storing the reversed string.

Checking for a palindrome in C is a great exercise for string manipulation.

By Using Two Pointers

In this method, two index pointers are taken: one pointing to the first character and other pointing to the last character in the string. The idea is to traverse the string in forward and backward directions simultaneously while comparing characters at the same distance from start and end.

  • If a pair of distinct characters is found, then the string is not palindrome.
  • If the two pointers meet at the middle of the string without any mismatched characters, then it is palindrome.


Let’s take a look at its implementation:

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

void isPalindrome(char *str) {
     
      // Index pointer to the start
    int left = 0;
  
    // Index pointer to the end
    int right = strlen(str) - 1;

    // Run the loop till both pointer
    // meet
    while (left < right) {
      
        // If characters don't match,
        // string is not palindrome
        if (str[left] != str[right]) {
            printf("\"%s\" is not palindrome.\n",
               str);
            return;
        }

        // Move both pointers towards
        // each other
        left++;
        right--;
    }

    // If all characters match,
    // string is palindrome
    printf("\"%s\" is palindrome.\n",
               str);
}

int main() {
    
      // Checking if given strings are palindrome
      isPalindrome("madam");
      isPalindrome("hello");
      return 0;
}

Output
"madam" is palindrome.
"hello" is not palindrome.

Time complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(1)

By Using Recursion

Two-pointer approach can also be implement using recursion. The current first and last index pointers (representing the current first and last characters) can be passed to the function as arguments.

  • If the characters at these pointers match, the function increments first pointer, decrements second pointer and then call itself again with updated pointers.
  • Otherwise, it returns false as the string is not palindrome.
  • If all the characters match till the first pointer is less than the last pointer, the string is palindrome so return true.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool palinHelper(char *s, int left, int right) {

    // If the start and end pointers cross
    // each other, it means all characters
    // have matched
    if (left >= right)
        return true;

    // If characters don't match,
    // string is not palindrome
    if (s[left] != s[right])
        return false;

    // Recursively check for the rest of
    // the string
    return palinHelper(s, left + 1, right - 1);
}

void isPalindrome(char* s) {
  
      // Calling the recursive function to check
    // palindrome string
    if (palinHelper(s, 0, strlen(s) - 1))
        printf("\"%s\" is palindrome.\n", s);
    else
        printf("\"%s\" is not palindrome.\n", s);
}

int main() {
  
    // Checking if the given strings are palindrome
    isPalindrome("madam");
    isPalindrome("hello");
    return 0;
}

Output
"madam" is palindrome.
"hello" is not palindrome.

Time complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(n), due to recursive stack space.



Next Article

Similar Reads

three90RightbarBannerImg