A palindrome is a string that is the same from right to left as from left to rig
ID: 3544382 • Letter: A
Question
A palindrome is a string that is the same from right to left as from left to right. For example, the following are palindromes:
ABADABA
RADAR
OTTO
MADAMIMADAM
EVE
For this exercise, we restrict each string to uppercase letters only. Develop a recursive function to test for palindromes. The function description is
// Postcondition: true has been returned if the substring of s at indexes i
// through j is a palindrome. Otherwise, false has been returned.
Hint: If i > = j, the substring of s at indices i through j is a (trivial) palindrome. Otherwise, that substring is a palindrome if and only if string(i) = string(j) and the substring of s at indices i + 1 through j - 1 is a palindrome.
Explanation / Answer
#include<stdio.h>
#include<string.h>
// Postcondition: true has been returned if the substring of s at indexes i
// through j is a palindrome. Otherwise, false has been returned.
int is_palindrome(char* str,int i, int j)
{
if(str[i]!=str[j])
return 0;
if(i == j) return 1;
else return(str,i+1,j-1);
}
int main()
{
char *str = "ABADABA";
char *str1 = "RADAR";
char *str2 = "OTTO";
char *str3 = "MADAMIMADAM";
char *str4 = "EVE";
char *str5 = "I AM NOT";
printf(" is %s is palindrome ? %s",str,(is_palindrome(str,0,strlen(str)-1)?"true":"false"));
printf(" is %s is palindrome ? %s",str1,(is_palindrome(str1,0,strlen(str1)-1)?"true":"false"));
printf(" is %s is palindrome ? %s",str2,(is_palindrome(str2,0,strlen(str2)-1)?"true":"false"));
printf(" is %s is palindrome ? %s",str3,(is_palindrome(str3,0,strlen(str3)-1)?"true":"false"));
printf(" is %s is palindrome ? %s",str4,(is_palindrome(str4,0,strlen(str4)-1)?"true":"false"));
printf(" is %s is palindrome ? %s",str5,(is_palindrome(str5,0,strlen(str5)-1)?"true":"false"));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.