Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
C++ Program to Check if a Given String is Palindrome or Not
Next article icon

C Program to Check for Palindrome String

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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.



Next Article
C++ Program to Check if a Given String is Palindrome or Not
author
kartik
Improve
Article Tags :
  • C Language
  • C Programs
  • DSA
  • Strings
  • Basic Coding Problems
  • C Basic Programs
  • C-String
  • FactSet
  • Morgan Stanley
  • palindrome
  • Paytm
  • TCS
Practice Tags :
  • FactSet
  • Morgan Stanley
  • Paytm
  • TCS
  • palindrome
  • Strings

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”. Properties of a Palindrome String:A palindrome string has some properties which are mentioned below: A palindrome string has a symmetric structure which means that the
    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 palindrome Input: s = "abc" Output: 0Explanation: s is not a palindrome Using Two-Pointers - O(n) time and O(1) spaceThe idea is to keep two pointers, one at the beginning (left)
    14 min read
  • Check Palindrome by Different Language

    • Palindrome Number Program in C
      Write a C program to check whether a given number is a palindrome or not. Palindrome numbers are those numbers which after reversing the digits equals the original number. Examples Input: 121Output: YesExplanation: The number 121 remains the same when its digits are reversed. Input: 123Output: NoExp
      4 min read

    • C Program to Check for Palindrome String
      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 tem
      4 min read

    • C++ Program to Check if a Given String is Palindrome or Not
      A string is said to be palindrome if the reverse of the string is the same as the original string. In this article, we will check whether the given string is palindrome or not in C++. Examples Input: str = "ABCDCBA"Output: "ABCDCBA" is palindromeExplanation: Reverse of the string str is "ABCDCBA". S
      4 min read

    • Java Program to Check Whether a String is a Palindrome
      A string in Java can be called a palindrome if we read it from forward or backward, it appears the same or in other words, we can say if we reverse a string and it is identical to the original string for example we have a string s = "jahaj " and when we reverse it s = "jahaj"(reversed) so they look
      8 min read

    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
    geeksforgeeks-footer-logo
    Corporate & Communications Address:
    A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
    Registered Address:
    K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
    GFG App on Play Store GFG App on App Store
    Advertise with us
    • Company
    • About Us
    • Legal
    • Privacy Policy
    • In Media
    • Contact Us
    • Advertise with us
    • GFG Corporate Solution
    • Placement Training Program
    • Languages
    • Python
    • Java
    • C++
    • PHP
    • GoLang
    • SQL
    • R Language
    • Android Tutorial
    • Tutorials Archive
    • DSA
    • Data Structures
    • Algorithms
    • DSA for Beginners
    • Basic DSA Problems
    • DSA Roadmap
    • Top 100 DSA Interview Problems
    • DSA Roadmap by Sandeep Jain
    • All Cheat Sheets
    • Data Science & ML
    • Data Science With Python
    • Data Science For Beginner
    • Machine Learning
    • ML Maths
    • Data Visualisation
    • Pandas
    • NumPy
    • NLP
    • Deep Learning
    • Web Technologies
    • HTML
    • CSS
    • JavaScript
    • TypeScript
    • ReactJS
    • NextJS
    • Bootstrap
    • Web Design
    • Python Tutorial
    • Python Programming Examples
    • Python Projects
    • Python Tkinter
    • Python Web Scraping
    • OpenCV Tutorial
    • Python Interview Question
    • Django
    • Computer Science
    • Operating Systems
    • Computer Network
    • Database Management System
    • Software Engineering
    • Digital Logic Design
    • Engineering Maths
    • Software Development
    • Software Testing
    • DevOps
    • Git
    • Linux
    • AWS
    • Docker
    • Kubernetes
    • Azure
    • GCP
    • DevOps Roadmap
    • System Design
    • High Level Design
    • Low Level Design
    • UML Diagrams
    • Interview Guide
    • Design Patterns
    • OOAD
    • System Design Bootcamp
    • Interview Questions
    • Inteview Preparation
    • Competitive Programming
    • Top DS or Algo for CP
    • Company-Wise Recruitment Process
    • Company-Wise Preparation
    • Aptitude Preparation
    • Puzzles
    • School Subjects
    • Mathematics
    • Physics
    • Chemistry
    • Biology
    • Social Science
    • English Grammar
    • Commerce
    • World GK
    • GeeksforGeeks Videos
    • DSA
    • Python
    • Java
    • C++
    • Web Development
    • Data Science
    • CS Subjects
    @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
    Lightbox
    Improvement
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
    geeksforgeeks-suggest-icon
    Create Improvement
    Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
    geeksforgeeks-improvement-icon
    Suggest Changes
    min 4 words, max Words Limit:1000

    Thank You!

    Your suggestions are valuable to us.

    What kind of Experience do you want to share?

    Interview Experiences
    Admission Experiences
    Career Journeys
    Work Experiences
    Campus Experiences
    Competitive Exam Experiences