Question
Print Non-Palindrome words in given sentence using recursion
ENTER THE SENTENCE
MOM AND DAD ARE NOT AT HOME
NON-PALINDROME WORDS IN GIVEN SENTENCE:
AND
ARE
NOT
AT
HOME
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class NonPalindromeWordsInSentence
{
public static String FindReverse(String wd,int i)
{
if(i>=0)
{
return wd.charAt(i)+ FindReverse(wd,i-1);
}
else
{
return "";
}
}
public static void main()
{
int i=0;
char ch=' ';
String sen="",word="";
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE SENTENCE");
sen=sc.nextLine();
sen=sen+" ";
System.out.println("NON-PALINDROME WORDS IN GIVEN SENTENCE:");
for(i=0;i< sen.length();i++)
{
ch=sen.charAt(i);
if(ch==' ')
{
if(word.equals(FindReverse(word,word.length()-1))==false)
{
System.out.println(word);
}
word="";
}
else
{
word=word+ch;
}
}
}
}