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
#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;
}
You can also try this code with Online C Compiler
Run Code
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.