Palindrome words remain the same after reversing also. Like “mom”–>after you reverse the word it is “mom” only.

Write a program to accept a sentence and count the number of palindrome words present in the sentence. Palindrome words are the words that remain same after reversing also. For example mom

Check some more java programs
https://java4school.com/julian-day-program

import java.io.*;
public class PalindromeWords
{
    String str="";
    void compute()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the sentence");
        str=br.readLine();
        if(str.endsWith("."))
            str=str.substring(0,str.length()-1);
        int count=0;
        int len=str.length();
        String st="";
        String arr[]=str.split("\\s+");
        int l=arr.length;
        for(int i=0;i<l;i++)
        {
            st=arr[i];

            if(palindrome(st)==true)
                count++;
        }
        System.out.println("The number of palindrome words are"+"  "  +  ""+count);
    }

    boolean palindrome(String c)
    {
        int n=c.length();
        String s="";
        for(int i=(n-1);i>=0;i--)
        {
            s+=c.charAt(i);
        }
        s=s.trim();
        if(s.equalsIgnoreCase(c))
            return true;
        else
            return false;
    }

    public static void main()throws IOException
    {
        PalindromeWords obj=new PalindromeWords();
        obj.compute();
    }
}    

Output:
Enter the sentence
My mom and dad are very kind.
The number of palindrome words are 2

Watch java videos here
https://www.youtube.com/user/trushnatej

LEAVE A REPLY

Please enter your comment!
Please enter your name here