In this BlueJ Program user will enter a sentence and a word. BlueJ Program will count the number of occurrences of the word in the sentence, means the frequency of the word in the sentence.
The sentence must be terminated with a full
stop (.)
Sample Input:
Enter any sentence: This is my India and
India is heaven for me.
Enter any Word: India
Frequency of the searched word: 2
import java.util.*;
class Search
{
String str1,str2,str;
Scanner sc=new Scanner(System.in);
public void assign()
{
System.out.print("\nEnter any
sentence:");
str1=sc.nextLine();
System.out.print("\nEnter any
Word:");
str2=sc.nextLine();
}
public void display()
{
int
len;
int
c=0;
len=str1.length();
if(str1.charAt(len-1)!='.')
{
System.out.println("Not followed the rule...");
return;
}
str1=str1.substring(0,len-1);
str1=str1 + " ";
while(true)
{
int i=str1.indexOf(' ');
if(i< 0)
break;
str=str1.substring(0,i);
str1=str1.substring(i+1);
if(str.equals(str2))
c++;
}
System.out.print("\nFrequency of the word = " + c);
}
public static void main(String args[])
{
Search ob=new Search();
ob.assign();
ob.display();
}
No comments:
Post a Comment