Introduction
A palindrome is a string that reads the same backward as forward, such as "madam" or "racecar." This guide will show you how to write a C program to check if a given string is a palindrome.
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Checks if the string is a palindrome.
- Displays whether the string is a palindrome or not.
Example:
- Input: String = "madam"
- Output: "madam is a palindrome"
Example 2:
- Input: String = "hello"
- Output: "hello is not a palindrome"
Solution Steps
- Include the Standard Input-Output and String Libraries: Use
#include <stdio.h>
for standard input-output functions and#include <string.h>
for string functions. - Write the Main Function: Define the
main
function, which is the entry point of every C program. - Declare Variables: Declare variables to store the string, its length, and a flag to indicate if the string is a palindrome.
- Input the String: Use
scanf
orgets
to take input from the user for the string. - Check if the String is a Palindrome: Use a loop to compare characters from the beginning and end of the string.
- Display the Result: Use
printf
to display whether the string is a palindrome or not.
C Program
#include <stdio.h> #include <string.h> int main() { // Step 1: Declare variables to hold the string, its length, and a flag for palindrome checking char str[100]; int length, i; int isPalindrome = 1; // Assume the string is a palindrome // Step 2: Prompt the user to enter a string printf("Enter a string: "); gets(str); // Using gets to read the string including spaces // Step 3: Find the length of the string length = strlen(str); // Step 4: Check if the string is a palindrome for (i = 0; i < length / 2; i++) { if (str[i] != str[length - 1 - i]) { isPalindrome = 0; // If characters don't match, it's not a palindrome break; } } // Step 5: Display the result if (isPalindrome) { printf("%s is a palindrome\n", str); } else { printf("%s is not a palindrome\n", str); } return 0; // Step 6: Return 0 to indicate successful execution }
Explanation
Step 1: Declare Variables
- The variable
str
is declared to store the input string. The variablelength
is used to store the length of the string, andi
is used as a loop counter. The variableisPalindrome
is a flag that starts as1
(true) and will be set to0
(false) if the string is found not to be a palindrome.
Step 2: Input the String
- The program prompts the user to enter a string using
printf
. Thegets
function is used to read the string, allowing it to include spaces.
Step 3: Find the Length of the String
- The
strlen
function from thestring.h
library is used to calculate the length of the string.
Step 4: Check if the String is a Palindrome
- The program uses a
for
loop to compare characters from the beginning of the string with characters from the end:- If any characters do not match,
isPalindrome
is set to0
and the loop breaks, indicating that the string is not a palindrome.
- If any characters do not match,
Step 5: Display the Result
- After checking, the program displays whether the string is a palindrome using the
printf
function.
Step 6: Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example 1:
Enter a string: madam madam is a palindrome
Example 2:
Enter a string: hello hello is not a palindrome
Conclusion
This C program demonstrates how to check if a string is a palindrome by comparing characters from the beginning and end of the string. It covers basic concepts such as string manipulation, loops, and conditional statements, making it a useful example for beginners learning C programming.