Computer Science, asked by aysaranam, 7 months ago

Write a program in java to enter a sentence.Display the words which are only palindrome​

Attachments:

Answers

Answered by sameerpawankum25
12

Answer:

Sample Input : MOM AND DAD ARE NOT AT HOME

Sample Output : MOM DAD

import java.util.*;

class Pg200prog1

{

static void teja()

{

Scanner in = new Scanner(System.in);

System.out.print(“Enter the String : “);

String st = in.nextLine();

int l = st.length();

String bin = “”;

for(int i=0;i<l;i++)

{

char ch = st.charAt(i);

if(!(Character.isWhitespace(ch)))

{

bin=bin+ch;

continue;

}

else

if(Character.isWhitespace(ch))

{

int b = bin.length();

int d=bin.charAt(0);

int e = bin.charAt(b-1);

if(d==e)

System.out.print(bin+” “);

bin = “”;

}

}

}

}

Answered by Oreki
6

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