Question
Check whether a word is Palindrome using recursion.
A palindrome word is a word that reads the same backward as forward, such as madam or racecar.
ENTER THE WORD
POP
POP IS A PALINDROME WORD
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class PalindromeOfWord
{
public static String FindReverse(String wd,int i)
{
if(i<0)
{
return "";
}
else
{
return wd.charAt(i)+ FindReverse(wd,i-1);
}
}
public static void main()
{
String word="",reverse="";
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE WORD");
word=sc.next();
reverse=FindReverse(word,word.length()-1);
if(reverse.equals(word)==true)
{
System.out.println(word+ " IS A PALINDROME WORD");
}
else
{
System.out.println(word+ " IS NOT A PALINDROME WORD");
}
}
}
Python
def reverseOfWord(wd, position):
if(position<0):
return ""
else:
return wd[position]+reverseOfWord(wd,position=position-1)
if(__name__=="__main__"):
word=input("Enter a word:")
reversedWord=reverseOfWord(word,len(word)-1)
if(word==reverseOfWord):
print(word,"is a Palindrome word")
else:
print(word, "is not a Palindrome word")