Sunday, February 16, 2014

Java Program On Counting Frequency Of Words In A Sentence Without Using StringTokenizer



 In this program, user will enter any sentence and frequency of the words will be displayed.
 The only restriction in this program is that StringTokenizer class can not be used to break
 the sentence into words.


 import java.io.*;
 class Str1
 {
  String st[]=new String[20];
  String temp,s,s1,s2;
  int i,j,x=0,c=1;
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  public void takeString() throws Exception
  {
   System.out.println("Enter the sentence;");
   s=br.readLine().toUpperCase();
   s1=s;
   while(true)
   {
    i=s1.indexOf(' ');
    if(i<0 font="">
    break;
    s2=s1.substring(0,i);
    s1=s1.substring(i+1);
    st[x++]=s2;
    }
    st[x++]=s1;
    for(i=0;i<=x-2;i++)
    {
     for(j=i+1;j<=x-1;j++)
     {
      if(st[i].compareTo(st[j])>0)
      {
       temp=st[i];
       st[i]=st[j];
       st[j]=temp;
       }
       }
       }
       temp=st[0];
       for(i=1;i<=x-1;i++)
       {
      if(temp.equals(st[i]))
      c++;
      else
      {
       System.out.println("Frequency of "+temp+" :"+c);
       c=1;
       temp=st[i];
      }
      }
      System.out.println("Frequency of "+temp+" :"+c);
      }
      public static void main(String args[])throws Exception
      {
       Str1 ob=new Str1();
       ob.takeString();
       }
       }

Related Post:  BlueJ Programs on String/Sentence

3 comments:

  1. Sir please give the suggestions for this years isc computer practical !!! please sir !

    ReplyDelete
  2. Sir can u help me out with the ' this ' keyword

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner