Introduction
A palindrome is a sequence that reads the same forward and backward. In programming, checking for palindromes is a common problem used to enhance logical thinking. In this guide, we will explore how to write a Palindrome Program in C, along with different approaches, examples, and explanations.
What is a Palindrome?
A palindrome can be a number, string, or sequence that remains unchanged when reversed. Some examples include:
- Numbers: 121, 1331, 454
- Strings: “madam”, “level”, “racecar”
- Sentences (ignoring spaces and punctuation): “A man, a plan, a canal, Panama!”
Methods to Check for Palindromes in C
There are multiple ways to check for a palindrome in C. We will explore the following:
- Using Loops (Iterative Approach)
- Using Recursion
- Using String Functions
1. Palindrome Program for Numbers (Using Loops)
This approach reverses the given number and compares it with the original.
Code Example:
#include <stdio.h>
int main() {
int num, reversed = 0, remainder, original;
// Input from user
printf("Enter a number: ");
scanf("%d", &num);
original = num;
// Reverse the number
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
// Check if original and reversed numbers are equal
if (original == reversed)
printf("%d is a palindrome.\n", original);
else
printf("%d is not a palindrome.\n", original);
return 0;
}
Explanation:
- The program takes an integer as input.
- It reverses the number by extracting digits.
- It checks if the reversed number matches the original.
- If they match, it prints “Palindrome”; otherwise, “Not a Palindrome.”
2. Palindrome Check Using Recursion (For Numbers)
Recursion is another approach where the function calls itself to reverse the number.
Code Example:
#include <stdio.h>
int reverseNumber(int num, int reversed) {
if (num == 0)
return reversed;
return reverseNumber(num / 10, reversed * 10 + num % 10);
}
int main() {
int num, reversed;
printf("Enter a number: ");
scanf("%d", &num);
reversed = reverseNumber(num, 0);
if (num == reversed)
printf("%d is a palindrome.\n", num);
else
printf("%d is not a palindrome.\n", num);
return 0;
}
Explanation:
- The
reverseNumber
function recursively builds the reversed number. - The base condition (
num == 0
) stops recursion. - The reversed number is compared to the original number.
3. Palindrome Program for Strings (Using String Functions)
Instead of numbers, we can check if a given string is a palindrome using the strlen
function from <string.h>
.
Code Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, flag = 1;
// Input from user
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 = 0;
break;
}
}
if (flag)
printf("%s is a palindrome.\n", str);
else
printf("%s is not a palindrome.\n", str);
return 0;
}
Explanation:
- The program takes a string as input.
- It calculates the length using
strlen
. - A loop checks characters from both ends moving toward the center.
- If any mismatch occurs, the flag is set to
0
, meaning it is not a palindrome.
Key Takeaways
- Palindrome checking is a fundamental concept in programming.
- Iterative and recursive methods can be used for numbers.
- String functions simplify palindrome checks for words.
- This concept is widely used in data validation, encryption, and pattern recognition.
Ready to Master C Programming?
Understanding core concepts like palindromes can sharpen your coding skills. If you want to build a strong foundation in Data Structures and Algorithms, check out our Data Science Course and kickstart your career! Apply Now
Conclusion
Palindrome programs are a great way to improve logical thinking and understand string and number manipulations in C. Whether using loops, recursion, or string functions, these techniques offer different approaches to solving the problem efficiently.
🔎 Take the Data Science Entrance Test – Boost your career with Cuvette’s placement guarantee! Take the test, and our team will contact you: Start the test now