Q:

C program to check a string is palindrome or not using recursion

belongs to collection: C String Programs

0

C program to check a string is palindrome or not using recursion

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Given a string, we have to check whether the given string is palindrome or not using recursion.

Program:

The source code to check a string is palindrome or not using recursion is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to check a string is palindrome or not
// using recursion

#include <stdio.h>
#include <string.h>

void isPalindrom(char str[], int index)
{
    int len = strlen(str) - (index + 1);

    if (str[index] == str[len]) {
        if (index + 1 == len || index == len) {
            printf("Given string is a palindrome\n");
            return;
        }
        isPalindrom(str, index + 1);
    }
    else {
        printf("Given string is not a palindrome\n");
    }
}

int main()
{
    char str[32] = { 0 };
    char rev[32] = { 0 };

    int cnt = 0;
    int flg = 0;

    printf("Enter a string: ");
    scanf("%s", str);

    isPalindrom(str, 0);

    return 0;
}

Output:

RUN 1:
Enter a string: malayalam
Given string is a palindrome

RUN 2:
Enter a string: abcdcba
Given string is a palindrome

RUN 3:
Enter a string: hello
Given string is not a palindrome

Explanation:

In the above program, we created two functions isPalinfrom() and main() function. The isPalindrom() is a recursive function to check specified string is palindrome or not.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to print the biggest and smallest palind... >>
<< C program to check a string is palindrome or not w...