Computer Applications

Write a program in Java to enter a sentence. Display the words which are only palindrome.
Sample Input: MOM AND DAD ARE NOT AT HOME
Sample Output: MOM
                          DAD

Java

Java String Handling

141 Likes

Answer

import java.util.Scanner;

public class KboatPalinWords
{
    public static void main(String args[]) {
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a sentence:");
       String str = in.nextLine();
       str = str + " ";
       String word = "";
       int len = str.length();
       
       for (int i = 0; i < len; i++) {
           char ch = str.charAt(i);
           if (ch == ' ') {
               int wordLen = word.length();
               boolean isPalin = true;
               for (int j = 0; j < wordLen / 2; j++) {
                   if (word.charAt(j) != word.charAt(wordLen - 1 - j)) {
                       isPalin = false;
                       break;
                   }
               }
               
               if (isPalin)
                    System.out.println(word);
                    
               word = "";
           }
           else {
               word += ch;
           }
       }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

44 Likes


Related Questions