S.No: 17 Exp. Name: Write a Java program to make frequency count of words in a given text.
Date: 2022-06-15
ID: 20095A0363
Page No:
Aim:
Write a Java program to make frequency count of words in a given text.
Source Code:
WordCount.java
import java.io.*;
class WordCount {
public static void main(String args[])throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter some text: ");
String s = br.readLine();
System.out.print("Enter the word whose frequency is to be found: ");
String sub = br.readLine();
int ind,count = 0;
String str[] = s.split(" ");
for(int i = 0;i<str.length;i++)
if(str[i].equals(sub))
count++;
System.out.println("The frequency of word '"+ sub +"' is: "+count);
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous) 2019-2023-MECH-FDH
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter some text: The first second was alright, but the second second was tough
Enter the word whose frequency is to be found: second
The frequency of word 'second' is: 3
Test Case - 2
User Output
Enter some text: Hello world! Hello John! Hello everyone!
Enter the word whose frequency is to be found: Hello
The frequency of word 'Hello' is: 3