Write a Java Program to make frequency count of words in a given text.

Program Code in JAVA:
import java.io.*;
import java.util.*;
class Freq
{
public static void main(String[] args) throws Exception
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter a String :");
String s=dis.readLine();
StringTokenizer st=new StringTokenizer(s);
int count=1,i=0;
int size=st.countTokens();
String words[]=new String[size];
while(st.hasMoreTokens())
{
words[i]=st.nextToken();
i++;
}
for(i=0;i<size;i++)
{
count=1;
for(int j=i+1;j<size;j++)
{
if(words[i].equals(words[j]))
{
count++;
move(words,j,size);
size--;
j--;
}
}
System.out.println(words[i]+" occurs "+count+" no of times");
}
}
static void move(String a[],int i,int size)
{
while(i<size)
{
if(i==size-1)
a[i]=null;
else
a[i]=a[i+1];
i++;
}
}
}

Program Output:
c:\java>javac Freq.java
c:\java>java Freq
Enter a String :
jai bolo hanuman ki jai
jai occurs 2 no of times
bolo occurs 1 no of times
hanuman occurs 1 no of times
ki occurs 1 no of times
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: