Palindrome in C: A Comprehensive Guide with Examples
Introduction
A palindrome is a sequence of characters that reads the same forward as backward. In this blog post, we’ll explore the concept of palindromes and delve into the details of writing a C program to determine whether a given string or number is a palindrome. We’ll cover the definition of palindromes, discuss various approaches to checking for palindromes, provide a step-by-step explanation of the algorithm, and present complete C code examples with outputs.
Understanding Palindromes
A palindrome can be a word, phrase, number, or other sequences of characters that remains unchanged when its order is reversed. Examples of palindromic words include “radar,” “level,” and “civic.” Additionally, palindromic phrases, such as “A man, a plan, a canal, Panama!” and palindromic numbers like “1221” and “12321,” exhibit symmetry when read in reverse.
Algorithm for Palindrome Checking
The algorithm for checking whether a given string or number is a palindrome involves comparing the characters from the beginning to the end and vice versa. Here’s a step-by-step breakdown of the algorithm:
Input: Receive the string or number to be checked for palindromicity.
Initialization: Set two pointers, one at the beginning and one at the end of the string or number.
Comparison: Compare the characters at the pointers. If they are equal, move the pointers towards each other. If they are not equal, the input is not a palindrome.
Repeat: Repeat the comparison and pointer movement until the pointers meet in the middle.
Palindrome Check: If the pointers meet, the input is a palindrome; otherwise, it is not.
Palindrome Program in C
Now, let’s implement the palindrome-checking algorithm in a C program. We’ll provide examples for both strings and numbers.
Palindrome Check for Strings
#include <stdio.h>
#include <string.h>
int isPalindromeString(char str[]) {
int start = 0;
int end = strlen(str) - 1;
while (start < end) {
if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Palindrome
}
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", inputString);
if (isPalindromeString(inputString)) {
printf("%s is a palindrome.\n", inputString);
} else {
printf("%s is not a palindrome.\n", inputString);
}
return 0;
}
Palindrome Check for Numbers
#include <stdio.h>
int isPalindromeNumber(int num) {
int originalNum = num;
int reversedNum = 0;
while (num > 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
return (originalNum == reversedNum);
}
int main() {
int inputNumber;
printf("Enter a number: ");
scanf("%d", &inputNumber);
if (isPalindromeNumber(inputNumber)) {
printf("%d is a palindrome.\n", inputNumber);
} else {
printf("%d is not a palindrome.\n", inputNumber);
}
return 0;
}
Output Examples
Example 1: Palindrome String
Enter a string: radar
radar is a palindrome.
Example 2: Non-Palindrome String
Enter a string: hello
hello is not a palindrome.
Example 3: Palindrome Number
Enter a number: 1221
1221 is a palindrome.
Example 4: Non-Palindrome Number
Enter a number: 12345
12345 is not a palindrome.
Conclusion
Palindromes are interesting sequences that exhibit symmetry. Writing a C program to check for palindromes involves a simple algorithm that compares characters from both ends. Whether you’re working with strings or numbers, the approach remains similar.
Feel free to experiment with the provided C code in your C development environment. If you have any questions, encounter issues, or want to explore more about palindromes or C programming, don’t hesitate to ask!