w3resource logo


C Programming Exercises

C Exercises: Check a given string is Palindrome or not

Secondary Nav

C Recursion : Exercise-16 with Solution

Write a program in C to Check whether a given string is Palindrome or not.
Test Data :
Input a word to check for palindrome : mom
Expected Output :

The entered word is a palindrome.

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

Flowchart :

Flowchart: Check a given word is Palindrome or not.

C Practice online:

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

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number