C Program to Check for Palindrome String
Last Updated :
10 Jan, 2025
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.
C
#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:
C
#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.
C
#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.
Similar Reads
Palindrome String Coding Problems A string is called a palindrome if the reverse of the string is the same as the original one.Example: âmadamâ, âracecarâ, â12321â.Palindrome StringProperties of a Palindrome String:A palindrome string has some properties which are mentioned below:A palindrome string has a symmetric structure which m
2 min read
Palindrome String Given a string s, the task is to check if it is palindrome or not.Example:Input: s = "abba"Output: 1Explanation: s is a palindromeInput: s = "abc" Output: 0Explanation: s is not a palindromeUsing Two-Pointers - O(n) time and O(1) spaceThe idea is to keep two pointers, one at the beginning (left) and
13 min read
Check Palindrome by Different Language
Easy Problems on Palindrome
Sentence Palindrome Given a sentence s, the task is to check if it is a palindrome sentence or not. A palindrome sentence is a sequence of characters, such as a word, phrase, or series of symbols, that reads the same backward as forward after converting all uppercase letters to lowercase and removing all non-alphanumer
9 min read
Check if actual binary representation of a number is palindrome Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation of the number is being considered for palindrome checking, no leading 0âs are being considered. Examples : Input : 9 Output : Yes (9)10 = (1001)2 Inp
6 min read
Print longest palindrome word in a sentence Given a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other t
14 min read
Count palindrome words in a sentence Given a string str and the task is to count palindrome words present in the string str. Examples: Input : Madam Arora teaches malayalam Output : 3 The string contains three palindrome words (i.e., Madam, Arora, malayalam) so the count is three. Input : Nitin speaks malayalam Output : 2 The string co
5 min read
Check if characters of a given string can be rearranged to form a palindrome Given a string, Check if the characters of the given string can be rearranged to form a palindrome. For example characters of "geeksogeeks" can be rearranged to form a palindrome "geeksoskeeg", but characters of "geeksforgeeks" cannot be rearranged to form a palindrome. Recommended PracticeAnagram P
14 min read
Lexicographically first palindromic string Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message "no palindromic string". Examples: Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string Simple Approach: 1. Sort the string charact
13 min read