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.
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.
Output:
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