w3resource logo
C Programming Exercises

C Exercises: Check a given string is Palindrome or not

C Recursion : Exercise-16 with Solution

Write a program in C to Check whether a given string is Palindrome or not.

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. 

Flowchart:

Flowchart: Check a given word is Palindrome or not.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.