Home Show/Hide Menu
Bluej Programs
➜ Test-paper-IT-10
➜ Suggestion Paper, Class-12

bluej Programs

5.Write a program that will accept a sentence which must end with either '.' or '!' or '?'.Now check and print the palindrome words and frequency of the palindrome words.
import java.util.*;
class palindromeWords
{
    Scanner sc = new Scanner (System.in);
    String sen;
    void input()
    {
        System.out.println("enter a sentence");
        sen = sc.nextLine().toUpperCase();
          int l=sen.length();
         char ch1= sen.charAt(l-1);
        if(ch1=='.' || ch1=='!' ||ch1=='?')
        System.out.println("the sentence ends with"+ch1);
        else
        {
            System.out.println("Wrong Input");
            System.exit(0);
        }
        sen=sen.substring(0,l-1);
        sen= sen+" ";
        String w="";
        int p=0,c=0;
        for(int i=0;i<sen.length();i++)
        {
            char ch= sen.charAt(i);
            if(ch==' ')
            {
             w="";   
            w=sen.substring(p,i);//System.out.println(w);
            
            if(palin(w)==true)
            {
                c++;
                System.out.println(w);
            }
            p=i+1;
        }
        }
        System.out.println("Frequency"+c);
             
    
}
    boolean palin(String w1)
    {
      String r=""; char ch2;
      for(int j=w1.length()-1;j>=0;j--)
      {
          ch2=w1.charAt(j);
          r=r+ch2;
      }
     
      if(r.equals(w1)== true)
      return true;
      else
      return false;
       
    }
    void main()
    {
     palindromeWords pw = new palindromeWords();
     pw.input();
    }
}


Output:
enter a sentence
mom and dad are in the madam home.
the sentence ends with.
MOM
DAD
MADAM
Frequency3


 

ADVERTISEMENT