C Program to Check whether a String is Palindrome or not using Recursion

This is a C program to check whether a given string is palindrome or not using recursion.

Problem Description

The following C program, with recursion, determines whether the entered string is a palindrome or not.

Problem Solution

A palindrome is a word, phrase or sentence that reads the same backward or forward.

Program/Source Code

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");
    }
}
Program Explanation

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.

advertisement
advertisement

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
$ 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

advertisement
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.