Write a recursive algorithm isPalindrome that takes as input a list S [0.. n ? 1
ID: 672313 • Letter: W
Question
Write a recursive algorithm isPalindrome that takes as input a list S[0..n ? 1] representing a string and returns True if it is a Palindrome or False if it is not.
Note: Your algorithm should contain no loops.
Algorithm 1 Determines if a given Word is a palindrome
input: Word, a word represented as a list L[0, n ? 1]
output: True if the word is a palindrome, False otherwise
i ? 0
j?n-1
while j > i do
return False
end if
i?i+1
j?j-1
end while
return True
This is my psudocode, how to make it recursive ?
Explanation / Answer
//Program is successfully executed in visual studio 2010
#include <stdio.h>
#include <string.h>
#include <conio.h>
void check(char [], int);
int main()
{
char word[15];
printf("Enter a word to check if it is a palindrome ");
scanf("%s", word);
check(word, 0);
getch();
return 0;
}
void check(char word[], int index)
{
int len = strlen(word) - (index + 1);
if (word[index] == word[len])
{
if (index + 1 == len || index == len)
{
printf("The entered word is a palindrome ");
return;
}
check(word, index + 1);
}
else
{
printf("The entered word is not a palindrome ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.