155k views
0 votes
A palindrome is a sequence that is equivalent to its reverse. Write a recursive function that determines if A[i], ..., A[j] is a palindrome.

1 Answer

4 votes

Answer:

#include <stdio.h>

#include <string.h>

#include <stdbool.h>

// A recursive function that

// check a str[s..e] is

// palindrome or not.

bool isPalRec(char str[], int s, int e)

{ // If there is only one character

if (s == e)

return true;

// If first and last

// characters do not match

if (str[s] != str[e])

return false;

// If there are more than

// two characters, check if

// middle sub-string is also

// palindrome or not.

if (s < e + 1)

return isPalRec(str, s + 1, e - 1);

return true;

}

bool isPalindrome(char str[])

{

int n = strlen(str);

// An empty string is

// considered as palindrome

if (n == 0)

return true;

return isPalRec(str, 0, n - 1);

}

// Driver Code

int main()

{

char str[50];

scanf("%[^\\]", &str);

if (isPalindrome(str))

printf("Number is a Palindrome :");

else

printf("Number is not a Palindrome : " );

return 0;

}

User MoustafaS
by
6.2k points