Introduction
A palindrome is a string that reads the same forward and backward. For example, "radar" and "level" are palindromes. This guide will show you how to write a C program to check if a string is a palindrome using recursion.
Example:
- Input: "madam"
- Output: "The string is a palindrome."
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Uses a recursive function to check if the string is a palindrome.
- Displays whether the string is a palindrome or not.
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 manipulation. - Write the Recursive Function: Define a recursive function that checks if the string is a palindrome by comparing characters from both ends.
- Write the Main Function: Define the
main
function to take user input, call the recursive function, and display the result. - Input the String: Use
gets
orscanf
to take input from the user for the string. - Call the Recursive Function: Pass the string and its indices to the recursive function to check if it is a palindrome.
- Display the Result: Use
printf
to display whether the string is a palindrome.
C Program to Check if a String is a Palindrome Using Recursion
#include <stdio.h> #include <string.h> // Step 2: Define the recursive function to check if the string is a palindrome int isPalindrome(char str[], int start, int end) { if (start >= end) { return 1; // Base case: If start index is greater than or equal to end index, it's a palindrome } if (str[start] != str[end]) { return 0; // If characters at start and end don't match, it's not a palindrome } // Recursively check the remaining substring return isPalindrome(str, start + 1, end - 1); } int main() { // Step 3: Declare a variable to hold the string char str[100]; // Step 4: Prompt the user to enter the string printf("Enter a string: "); gets(str); // Using gets to read the string including spaces // Step 5: Call the recursive function to check if the string is a palindrome int result = isPalindrome(str, 0, strlen(str) - 1); // Step 6: Display the result if (result) { printf("The string is a palindrome.\n"); } else { printf("The string is not a palindrome.\n"); } return 0; // Return 0 to indicate successful execution }
Explanation
Step 2: Define the Recursive Function
- The
isPalindrome
function takes three parameters:str[]
: The string to be checked.start
: The starting index for the current recursion step.end
: The ending index for the current recursion step.
- Base Case: If the
start
index is greater than or equal to theend
index, the recursion stops, and the function returns 1, indicating that the string is a palindrome. - Recursive Case:
- If the characters at the
start
andend
indices do not match, the function returns 0, indicating that the string is not a palindrome. - Otherwise, the function calls itself recursively with the
start
index incremented by 1 and theend
index decremented by 1.
- If the characters at the
Step 3: Declare a Variable
- The variable
str
is declared to store the input string.
Step 4: Input the String
- The program prompts the user to enter a string using
gets
, which reads the entire line of input, including spaces.
Step 5: Call the Recursive Function
- The program calls the
isPalindrome
function with the string, the starting index0
, and the ending indexstrlen(str) - 1
.
Step 6: Display the Result
- The program checks the value of
result
:- If
result
is 1, the string is a palindrome. - If
result
is 0, the string is not a palindrome.
- If
Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example 1:
Enter a string: madam The string is a palindrome.
Example 2:
Enter a string: hello The string is not a palindrome.
Conclusion
This C program demonstrates how to check if a string is a palindrome using a recursive function. It covers basic concepts such as recursion, string manipulation, and user input, making it a useful example for beginners learning C programming.