Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

C Program to Check for Palindrome String

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Palindromes are like a fun puzzle in the world of programming. They're those strings that read the same backward & forward, like "level" or "radar". In this article, we're looking into how to check for palindrome strings in C, a popular programming language. 

C Program to Check for Palindrome String

We'll walk through an algorithm, craft a program from scratch, & by the end, you'll be equipped with the knowledge to solve the concept of palindrome on your own.

Algorithm (Step by Step Explanation)

When we talk about checking for palindrome strings in C, it's like following a recipe, but simpler. Let's break it down step by step:

  • Start with Input: First, we need a string. It's what we're going to check. Think of it as our test subject.
     
  • Find the Length: We measure the string, not with a ruler, but by counting how many characters it has. This helps us know where the string ends.
     
  • Set Up the Checkpoints: Imagine standing at both ends of a bridge. One end is the start of the string, & the other end is the finish. We're going to walk towards the middle from both ends.
     
  • Begin the Comparison: Now, we start comparing letters. The first character with the last, the second with the second last, & so on. If they all match as we move towards the center, we're on the right track.
     
  • Reach the Middle: Once we're in the middle & every character has matched its counterpart, it's a palindrome. If even one pair doesn't match, it's not.
     

This approach ensures that we thoroughly check the string, from start to finish, confirming its palindrome status without missing a beat.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Palindrome String Program in C

To put our algorithm into action, we'll write a C program. This program will check if a given string is a palindrome. Here’s how we do it:

Include Necessary Libraries

We start by including the standard input-output library, which lets us use input & output functions in our program.

#include <stdio.h>
#include <string.h>  // This library lets us work with strings, like finding their length.

Write the Main Function

Every C program starts with a main function. This is where our code kicks off.

int main() {

Declare Variables

We need a string (array of characters) to store our input & some variables to help us loop through the string.

char str[100];
int i, len, flag = 0;

Get User Input

We ask the user to input a string & store it in str.

printf("Enter a string: ");

gets(str);  // Reads a line of text from the user.

Find the Length & Start Checking

We use strlen() to get the string length & start comparing characters from both ends.

len = strlen(str);
for(i=0; i < len/2; i++) {
  if(str[i] != str[len-i-1]) {
    flag = 1;
    break;
  }
}

Display the Result

If flag is still 0, it means all characters matched, & it’s a palindrome. Otherwise, it's not.

if (flag == 0)
  printf("%s is a palindrome.\n", str);
else
  printf("%s is not a palindrome.\n", str);
End the Program:
We close the main function & the program.
return 0;
}


This C program follows our step-by-step algorithm to check if the input string is a palindrome. It's straightforward, using basic loops & string functions, making it easy to understand & run.

Now look at another C program on Palindrom but this time it’s a complete code.

  • C

C

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

int main() {
char str[100];
int i, len;
int flag = 0;

printf("Enter a string: ");
scanf("%s", str);

len = strlen(str);

for(i = 0; i < len / 2; i++) {
if(str[i] != str[len - i - 1]) {
flag = 1;
break;
}
}

if(flag) {
printf("%s is not a palindrome.\n", str);
} else {
printf("%s is a palindrome.\n", str);
}

return 0;
}

Output

Enter a string: Hello
Hello is not a palindrome.

This program directly takes a string from the user, checks each character from the start with its corresponding character from the end, and decides if the string is a palindrome. If any pair of characters doesn't match, the flag is set to 1, and the loop breaks early. Depending on the flag, the program then prints whether the string is a palindrome or not.

Frequently Asked Questions

What is a palindrome string?

A palindrome string is a sequence of characters that reads the same backward as forward. Examples include "madam" and "racecar".

Can numbers be palindromes too?

Yes, numbers can be palindromes as well. For instance, 12321 and 4554 are palindromic numbers.

Why is it important to learn about palindrome strings in programming?

Understanding palindrome strings helps improve problem-solving and algorithmic thinking skills, which are crucial in programming. It's a common challenge in coding interviews and competitions.

Conclusion

In this article, we've learned about palindrome strings and how to check for them in C. We started with a clear, step-by-step algorithm and then used that into writing a straightforward C program to implement our approach. This exercise not only helped us grasp checking for palindrome strings but also sharpened our programming skills with practical C code. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Topics covered
1.
Introduction
2.
Algorithm (Step by Step Explanation)
3.
Palindrome String Program in C
3.1.
Include Necessary Libraries
3.2.
Write the Main Function
3.3.
Declare Variables
3.4.
Get User Input
3.5.
Find the Length & Start Checking
3.6.
Display the Result
4.
Now look at another C program on Palindrom but this time it’s a complete code.
4.1.
C
5.
Frequently Asked Questions
5.1.
What is a palindrome string?
5.2.
Can numbers be palindromes too?
5.3.
Why is it important to learn about palindrome strings in programming?
6.
Conclusion