This is a C program to check whether a given string is palindrome or not using recursion.
The following C program, with recursion, determines whether the entered string is a palindrome or not.
A palindrome is a word, phrase or sentence that reads the same backward or forward.
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Check whether a given String is Palindrome or not * using Recursion */ #include <stdio.h> #include <string.h> void check(char [], int); int main() { char word[15]; printf("Enter a word to check if it is a palindrome\n"); scanf("%s", word); check(word, 0); return 0; } void check(char word[], int index) { int len = strlen(word) - (index + 1); if (word[index] == word[len]) { if (index + 1 == len || index == len) { printf("The entered word is a palindrome\n"); return; } check(word, index + 1); } else { printf("The entered word is not a palindrome\n"); } }
In this C program, we are reading the value of ‘string using word character array variable. The check() function is used to check the string is a palindrome or not. A palindrome is a word, phrase or sentence that reads the same backward or forward.
Compute the difference between the lengths of the word by the index variable value by increments one value. Then nested-If else condition statement is used to check the value of ‘word[]’ varaible with base index as index variable value is equal to the value of ‘word[]’ with base index len variable value.
If the condition is true then execute if condition statement. Another if condition statement is used to check the given string is palindrome using logical OR operator. Check the value of ‘index’ variable increment by 1 is equal to the value of ‘len’ variable and the value of ‘index’ variable is equal to the value of ‘len’ variable.
If the condition is true then execute the statement and print the entered word is a palindrome. And again call the check() function to complete the process for whole string. If the condition is false, then execute the else condition statement and print the statement as the entered word is not a palindrome.
$ gcc palindrome.c -o palindrome $ a.out Enter a word to check if it is a palindrome malayalam The entered word is a palindrome
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C Books
- Practice BCA MCQs
- Watch Advanced C Programming Videos