Question

Print Palindrome Words in given sentence using recursion

				
					ENTER THE SENTENCE
mom and dad are not at home.
PALINDROME WORDS IN GIVEN SENTENCE:
mom
dad
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class PalindromeWordsInSentence
{

    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("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))==true)
                {
                    System.out.println(word);

                }
                word="";
            }
            else
            {
                word=word+ch;   
            }
        }  

    }
}

  

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *