Computer Science, asked by sambitaojha78, 10 months ago

Using scanner class write a program to accept a sentence and display only palindrome word

Answers

Answered by Oreki
0

import java.util.Scanner;

public class StringManipulation {

static boolean isPalindrome(String word) {

return word.equals(new StringBuffer(word).reverse( ).toString( ));

}

public static void main(String[ ] args) {

// Accepting sentence and converting it into a String array.

System.out.print("Enter a Sentence - ");

String[ ] sentence = new Scanner(System.in).nextLine( ).split(" ");

// Printing only the words which are Palindrome.

for (String word : sentence)

if (isPalindrome(word))

System.out.println(word);

}

}

Similar questions