Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Display the words of a sentence in ascending order of their frequencies

 import java.io.*;
import java.util.*;
public class Gcd 
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int i,j,k,x,y;
    String str,sArr[],fArr[]; 
    StringTokenizer stk;
    int arr[];
    void accept() throws Exception
    {
        
        x=0;
        System.out.print("Enter a sentence ");
         str=br.readLine(); 
         stk=new StringTokenizer(str);
         i=stk.countTokens();
         sArr=new String[i];
         fArr=new String[i];
         arr=new int[i];
         while(stk.hasMoreTokens())
         {
             sArr[x++]=stk.nextToken();
            }
            for(i=0;i            {
                 for(j=i+1;j                 {
                     if(sArr[i].compareTo(sArr[j])>0)
                     {
                         str=sArr[i];
                         sArr[i]=sArr[j];
                         sArr[j]=str;
                        }
                    }
                }
                str=sArr[0];
                y=0;
                j=1;
                for(i=1;i                {
                    if(str.equals(sArr[i]))
                    j++;
                    else
                    {
                        fArr[y]=str;
                        arr[y]=j;
                        y++;
                        j=1;
                        str=sArr[i];
                    }
                }
                for(i=0;i                {
                     for(j=i+1;j                     {
                        if(arr[i]                        {
                            k=arr[i];
                            arr[i]=arr[j];
                            arr[j]=k;
                            str=fArr[i];
                            fArr[i]=fArr[j];
                            fArr[j]=str;
                        }
                    }
                }                    
                    
                System.out.println("\nWords in ascending order according to frequency");
                
                for(i=y-1;i>=0;i--)
                {                    
                     System.out.println("Frequency of "+ fArr[i] + " : "+arr[i]);                     
                    }
            }           
                           
public static void main(String args[])throws Exception
{
    Gcd ob=new Gcd();
    ob.accept();
}
}


Sample Input Output

Input: Enter a sentence this is a test of Ascending order of word frequenct and word and Frequency to display


Output: Words in ascending order according to frequency


Frequency of to : 1

Frequency of this : 1

Frequency of test : 1

Frequency of order : 1

Frequency of is : 1

Frequency of frequency : 1

Frequency of frequenct : 1

Frequency of display : 1

Frequency of ascending : 1

Frequency of a : 1

Frequency of of : 2

Frequency of and : 2




This post first appeared on Tutorial Site On Computer Programming Languages F, please read the originial post: here

Share the post

Display the words of a sentence in ascending order of their frequencies

×

Subscribe to Tutorial Site On Computer Programming Languages F

Get updates delivered right to your inbox!

Thank you for your subscription

×