Array-promo

Question

print smallest palindrome words in a given sentence.

Share code with your friends

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

Code

				
					
import java.util.Scanner;
public class smallestPalindromeWordsInSentence 
{  
    public static void main(String[] args)
    {  
        String sen="",wd="",wd1="",smallestPalindrome="";
        char ch=' ',ch1=' ';
        int i=0,len=0;
        Scanner sc = new Scanner(System.in);    
        
        System.out.println("Enter a sentence");   
        sen = sc.nextLine(); 
        sen=sen+" ";
        sen=sen.toLowerCase();
        len=sen.length();
        smallestPalindrome=sen;
        for(i=0;i< len;i++)
        {
            ch=sen.charAt(i);
            if(ch==' ')
            {
                
                if(wd.equals(wd1)==true)
                {
                    if(wd.length()< smallestPalindrome.length())
                    {
                        smallestPalindrome=wd;
                    }
                }
                wd1="";
                wd="";
                
            }
            else
            {
                wd=wd+ch;
                wd1=ch+wd1;
            }
        }
                System.out.println("Smallest Palindrome words in sentence:"+smallestPalindrome);   

    }  
}    

				
			

Coding Store