-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_frequency.java
37 lines (35 loc) · 1.22 KB
/
Word_frequency.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Write a program to enter a sentence from the keyboard and count the number of times a particular word occurs in it. Display the frequency of the search word.
// Example:
// INPUT:
// Enter a sentence : the quick brown fox jumps over the lazy dog.
// Enter a word to be searched : the
// OUTPUT :
// Searched word occurs : 2 times. [15]
import java.io.*;
class Word_frequency {
public static void main(String args[]) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
String str, word, wrd = "";
char ch;
int c = 0;
System.out.println("Enter A sentence");
str = br.readLine();
str = str.trim();
System.out.println("Enter a word to be searched");
word = br.readLine();
str = str + " ";
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (ch != ' ') {
wrd = wrd + ch;
} else {
if (word.equals(wrd)) {
c++;
}
wrd = "";
}
}
System.out.println("searched word occurs" + c);
}
}