C Program to Check Palindrome String
A palindrome is a word, phrase, number, or string that reads the same backward as forward. In C programming, we can write a program to check if a string entered by the user is a palindrome. This tutorial explains how to create a simple program to perform this check using basic string manipulation techniques.
Understanding Palindrome Strings
Examples of palindrome strings include 'radar', 'level', and 'madam'. The main logic to check a palindrome involves comparing characters from the beginning and end of the string. If all corresponding characters match until the middle of the string, the string is a palindrome; otherwise, it is not.
Algorithm to Check Palindrome String
- Start the program.
- Declare a string to store user input and other required variables.
- Prompt the user to enter a string.
- Read the string using scanf or fgets.
- Initialize two index variables: one starting from the beginning and one from the end of the string.
- Compare characters at both indexes.
- If all characters match, the string is a palindrome; else it is not.
- Display the result.
- End the program.
C Program Code
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int length, i, flag = 0;
printf("Enter a string: ");
scanf("%s", str);
length = strlen(str);
for(i = 0; i < length/2; i++) {
if(str[i] != str[length-i-1]) {
flag = 1;
break;
}
}
if(flag == 0)
printf("The string '%s' is a palindrome.\n", str);
else
printf("The string '%s' is not a palindrome.\n", str);
return 0;
}
Sample Output
Enter a string: radar The string 'radar' is a palindrome. Enter a string: hello The string 'hello' is not a palindrome.
Step-by-Step Explanation
- The program starts by including the stdio.h and string.h header files. string.h is required for the strlen() function to calculate the length of the string.
- A character array 'str' is declared to store the input string, along with integer variables for loop control and a flag.
- The user is prompted to enter a string using printf, and the input is read using scanf.
- The strlen() function is used to determine the length of the string.
- A for loop iterates from the start to the middle of the string, comparing characters from the beginning and the end.
- If any pair of characters do not match, the flag variable is set to 1 and the loop breaks, indicating the string is not a palindrome.
- After the loop, the program checks the flag variable and prints whether the string is a palindrome or not.
Important Notes
- This program checks for case-sensitive palindromes. For example, 'Radar' will not be considered a palindrome. To make it case-insensitive, convert all characters to lowercase using tolower().
- The current program uses scanf(), which stops input at whitespace. To check multi-word phrases, use fgets() instead of scanf().
- The maximum string length is set to 100 characters. Adjust the size according to your needs.
Practice Exercises
- Modify the program to check multi-word palindromes with spaces (e.g., 'nurses run').
- Create a function isPalindrome() that returns 1 if the string is a palindrome and 0 otherwise.
- Write a program to check if a number is a palindrome instead of a string.
- Make the palindrome check case-insensitive and ignore punctuation and spaces.
Conclusion
Checking palindrome strings in C is a great way for beginners to practice string handling, loops, and conditional statements. The logic involves comparing characters from both ends of the string and using a simple flag to determine the result. By modifying and practicing this program, you can explore more advanced string manipulation tasks in C programming.
Codecrown