C Exercises: Check a given string is Palindrome or not
C Recursion : Exercise-16 with Solution
Write a C program to check whether a given string is a palindrome or not using recursion.
Pictorial Presentation:

Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
void checkPalindrome(char [], int);
int main()
{
char wordPal[25];
printf("\n\n Recursion : Check a given string is Palindrome or not :\n");
printf("----------------------------------------------------------\n");
printf(" Input a word to check for palindrome : ");
scanf("%s", wordPal);
checkPalindrome(wordPal, 0);//call the function for checking Palindorem
return 0;
}
void checkPalindrome(char wordPal[], int index)
{
int len = strlen(wordPal) - (index + 1);
if (wordPal[index] == wordPal[len])
{
if (index + 1 == len || index == len)
{
printf(" The entered word is a palindrome.\n\n");
return;
}
checkPalindrome(wordPal, index + 1);//calling the function itself recursively
}
else
{
printf(" The entered word is not a palindrome.\n\n");
}
}
Sample Output:
Recursion : Check a given string is Palindrome or not : ---------------------------------------------------------- Input a word to check for palindrome : mom The entered word is a palindrome.
Explanation:
void checkPalindrome(char wordPal[], int index) { int len = strlen(wordPal) - (index + 1); if (wordPal[index] == wordPal[len]) { if (index + 1 == len || index == len) { printf(" The entered word is a palindrome.\n\n"); return; } checkPalindrome(wordPal, index + 1);//calling the function itself recursively } else { printf(" The entered word is not a palindrome.\n\n"); } }
This function ‘checkPalindrome()’ takes a string ‘wordPal’ and an integer ‘index’ as parameters. It checks if the given string is a palindrome or not by comparing the first and last characters of the string, and then recursively checks the remaining characters in the same way.
If the first and last characters match, the function calls itself with the updated index to check the remaining characters. If they do not match, the function returns "The entered word is not a palindrome." as output.
Time complexity and space complexity:
The time complexity of this function is O(n) since it traverses the string once, where n is the length of the string.
The space complexity of this function is O(n) because of the recursive calls, which create new stack frames each time, and the maximum number of stack frames that can be created is equal to the length of the string.
Flowchart:

C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a program in C to multiply two matrix using recursion.
Next: Write a program in C to calculate the power of any number using recursion.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
__FILE__ macro shows full path
#include <string.h> #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) For Windows use '\\' instead of '/'.
Ref : https://bit.ly/3iEWRoT
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook