LAST UPDATED: JANUARY 20, 2022
C Program to Check whether a given String is Palindrome or not using Recursion
The characters in the string should remain the same after reversing the sequence of characters, The word should be read the same in both forward and backward then the string is known as "Palindrome".
Logic To Check Whether The Given String Is Palindrome Or Not:
- Get the string from the user and store it in an array variable string.
- Comparing the length of the string using the index variable,
- Increment the index variable by 1,
- Using the nested if-else condition compare the length of the string with the base index value of the string,
- If the condition is satisfied, check whether the given string is palindrome or not using another if-else statement.
- If the condition is satisfied print the statement "The Given String Is Palindrome", If the condition is not satisfied print "The Given String Is Not Palindrome".
C Program To Check Whether The Given String Is Palindrome Or Not:
#include <stdio.h>
#include <string.h>
void palindrome(char [], int);
int main()
{
char string[15];
printf("Enter A String: ");
scanf("%s", string);
palindrome(string, 0);
return 0;
}
void palindrome(char string[], int index)
{
int len = strlen(string) - (index + 1);
if (string[index] == string[len])
{
if (index + 1 == len || index == len)
{
printf("The Given String Is A Palindrome\n");
return;
}
palindrome(string, index + 1);
}
else
{
printf("The Given String Is Not A Palindrome\n");
}
}
Output:
Case 1: If the given Text Is Palindrome,
Case 2: If the given Text Is Not Palindrome,