Tuesday, October 20, 2020

BlueJ Program On Counting Frequency Of A Word In Sentence

 In this BlueJ Program, Take any sentence and a particular word. Find the frequency of the word in the sentence.

import java.util.*;
class Frequency
{
  int i,c=0;
  String str,s,word;
Scanner sc=new Scanner(System.in);
public void take()
{
     System.out.print("\nEnter the sentence:");
     str=sc.nextLine();
     str=str+" ";
     System.out.print("\nEnter the word:");
     word=sc.nextLine();
     while(true)
     {
          i=str.indexOf(' ');
          if(i<0)
          break;
          s=str.substring(0,i);
          str=str.substring(i+1);
          if(s.equals(word))
          c++;
      }
      System.out.print("\nFrequency of the word "+word+ " is: "+c);
    }
    public static void main(String args[])
    {
         Frequency ob=new Frequency();
         ob.take();
    }

}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner