Palindrome Program in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Palindrome in c refers to the object that remains the same; even if we reverse the object, it remains the same as its original form. For example, "naman" is a palindrome as it remains the same when we reverse the string naman.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Algorithm

Logic to identify whether the given object is a palindrome or not is given below:

Algorithm 1

palindrome program algorithm

  1. Obtain the input object, which is to be checked whether it is palindrome or not.
  2. Now calculate the reverse of the input object and store it in another variable.
  3. Now check whether the original input object obtained is equal to the reverse calculated and store the result to the bool variable.
  4. If the calculated bool value is true, the given object is a palindrome; otherwise, the given object is not a palindrome.
  • Time Complexity: O(n)O(n) Here, we must traverse the whole string once; the time complexity is O(n).

  • Space Complexity: O(n)O(n) Here we need an extra space of the same array size as we need to reverse the string and store it; therefore, the space complexity is O(n).

Algorithm 2 palindrome image

  1. Obtain the input object, which is to be checked whether it is palindrome or not.
  2. Now, take two pointers, start pointer and end pointer. The start pointer points to the start of the input, and the end pointer points to the end.
  3. Now check the values corresponding to both the pointers. Increment the start pointer and decrement the end pointer if the value at both pointers is the same; otherwise, break as the input is not a palindrome.
  4. Repeat step 3 till either the start and end pointer point to the exact location or till the start pointer points to location (n/2), where n is the length of the input.
  • Time Complexity: O(n)O(n) We must traverse the whole string once, so the time complexity is O(n).

  • Space Complexity: O(1)O(1) We need extra space; therefore, the space complexity is O(1).

Palindrome Number Program in C

Sharpen Your Fundamentals with Free Learning

Algorithm 1

Here we would use a while loop to iterate over the string and reverse it. After reversing the string, we would compare both the strings and check whether it is a palindrome or not. For reversing, we would be traversing the whole string and storing its reverse in a new string rev using a while loop and after reversing, we would be comparing the string with the original string using strcmp() function, which returns 0 if both strings are identical and therefore if the string is same as reversed one, then it would return 0 which means that the given string is a Palindrome in c.

Code for Algorithm 1:

Note: Here in the above code we have used l-2 as the last character of the string as indexing is zero-based therefore for length l the last character would be at position l-1, but as this is a character array, therefore, it has an extra end line character 'null' as the last character for the last character of the string we would be using l-2.

Output:

Output Explanation This Palindrome program in c successfully reverses the input string 'cabbac' and compares it with the original. Since both are identical, the program concludes that 'cabbac' is a palindrome.

Complexity Analysis

  • Time Complexity: O(N)O(N), where 'N' is the length of the string, accounting for the time taken to reverse and compare the string.
  • Space Complexity: O(N)O(N), due to the additional space required for storing the reversed string.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Algorithm 2

Here, we would be comparing start and end pointers and comparing characters one by one using a while loop, and in case any of the pointers do not match, then the given string would not be a Palindrome in c.

Code for Algorithm 2:

Output:

Output Explanation The Palindrome program in C checks each character from the start and end of the string. If all corresponding characters match, it confirms the string as a Palindrome in C. Otherwise, it's not a Palindrome number in C. This efficient approach avoids extra space, making it ideal for palindrome verification.

Complexity Analysis:

  • Time Complexity: O(N)O(N), where 'N' is the length of the string, as each character is checked at most once.
  • Space Complexity: O(1)O(1), indicating constant space usage regardless of input size, highlighting the efficiency of this C program to check palindrome.

Conclusion

  • Palindrome numbers in C, like "121", remain unchanged when reversed, mirroring the behaviour of palindromic strings such as "naman".
  • In C, there are two approaches: either we can reverse the string and compare each character one by one.
  • Another way is to compare the first and the last characters one by one till either we reach the middle of the string or any characters at first and the last pointer are dissimilar.
  • Complexity varies with the method: reversing requires O(N) space and time, while end-to-end comparison uses O(1) space and O(N) time.