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

Program Code:
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++;
}
}
}
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: