Answer to Question #295556 in Java | JSP | JSF for Theder

Question #295556

Write a program to take a String as input then display only palindrome words found in the String and also display how many palindrome words found.

For example:

Enter a String

MOM AND dad are talking

Palindrome words: MOM dad

No. of such words: 2


for(i=s.length()-1;i>=0;i--)

{

c=s.charAt(i);

rev=rev+c;

} //To check palindrome words


1
Expert's answer
2022-02-09T10:40:56-0500
import java.util.*;


class App {


	static boolean palindromeWord(String word) {
		String rev = "";
		for (int i = word.length() - 1; i >= 0; i--) {
			char c = word.charAt(i);
			rev = rev + c;
		} // To check palindrome words
		return rev.compareToIgnoreCase(word) == 0;
	}


	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a String: ");
		String inputWords[] = in.nextLine().split(" ");
		int noWords = 0;
		System.out.print("Palindrome words: ");
		for (int i = 0; i < inputWords.length; i++) {
			if (palindromeWord(inputWords[i])) {
				System.out.print(inputWords[i] + " ");
				noWords++;
			}
		}
		System.out.println("\nNo. of such words: " + noWords);


		in.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS