Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive function that takes a string of length n and returns 1 if the

ID: 3787877 • Letter: W

Question

Write a recursive function that takes a string of length n and returns 1 if the string is a palindrome, 0 if the string is not a palindrome. A palindrome is a string that is spelled the same forwards as it is backwards. E.g., "abba" and "aba" are both palindromes, but "abb" is not. The empty string, "", is also a palindrome. For simplicity, you may assume the string passed to your function contains lowercase letters only, and no spaces. Do not call strlen() under any circumstances! The function signature is: int isPalindrome(char * str, int n);

Explanation / Answer

#include <stdio.h>

int palindrome(char *str, int n){
int i;
for(i=0; i<n/2; i++){
if(str[i] != str[n-1-i]){
return 0;
}
}
return 1;
}

int main()
{
char s[100];
int i, n =0;
printf("Enter the string: ");
scanf("%s", &s);
for(i=0; s[i]!=''; i++){
n++;
}
if(palindrome(s, n)){
printf("Given string is a palindrome ");
}
else{
printf("Given string is not a palindrome ");
}

return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the string: abba                                                                                                                                                                                                                                 

Given string is a palindrome                                                                                                                                                                                                                           

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the string: aba                                                                                                                                                                                                                                  

Given string is a palindrome                                                                                                                                                                                                                           

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the string: abb                                                                                                                                                                                                                                  

Given string is not a palindrome                                                                                                                                                                                                                       

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the string: madam                                                                                                                                                                                                                                

Given string is a palindrome

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote