Monday, August 19, 2019

Frequency Of An Entered Word In A Sentence



This is a BlueJ program to count the frequency of a particular word from a sentence.

              Write a program to enter a sentence from user and a word. Display the frequency of the word in the sentence
    
import java.io.*;
class Sentence
{
int i,c=0;
String sentence,s1,word[]=new String[20];
int x=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void disp()throws Exception
{
System.out.println("Enter the sentence:");
sentence=br.readLine().trim()+" ";
System.out.println("Enter the Word:");
s1=br.readLine().trim();
while(true)
{
i=sentence.indexOf(' ');
if(i< 0)
break;
word[x++]=sentence.substring(0,i);
sentence=sentence.substring(i+1);
}
for(i=0;i<=x-1;i++)
{
if(word[i].equals(s1))
 c++;
}
System.out.println(s1+ " frequence="+c);


}
public static void main(String args[])throws Exception
{
Sentence ob=new Sentence();
ob.disp();
}
}


Related Post: BlueJ Programs on String/Sentence

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner