PROBLEM 4: COUNT THE KEYWORD
To find out and count manually the
keyword in the passage is not easy. Imagine if the passage is more
than 10 pages and you have to read through the whole passage from the
first page until at the end of the passage but the keyword that you
search is not found. You will be frustrated. As an expert in computer
programming you are required to write a program to find and count how
many times the keyword repeats in the passage.
Input
The first line of input is an Integer
number (1 ≤ N ≤ 10) which indicate the test cases (number of
passages). The second line is the keyword (String). Following the
second line are the test cases. Each line in a test case contains a
word, phrase or passage.
The input must be read from standard
input.
Output
The output is either YES or NO and
followed by how many times the keyword found in the passage.
Sample
Input
|
Sample
Output
|
4
Computer
Yesterday
i bought 2 computer and ipad.
Computer
and commuter are different things.
Computer
Sciences and Computer Multimedia.
Bachelor
in Business Computing.
|
YES
1
YES
1
YES
2
NO
0
|
Solution :
import java.util.*;
public class KeyWord {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
String ls = System.getProperty("line.separator");
scan.useDelimiter(ls);
int no = scan.nextInt();
String var = scan.next();
for(int x=0;x<no;x++) {
String sentence=scan.next();
int total = 0;
StringTokenizer st = new StringTokenizer(sentence);
while(st.hasMoreTokens()) {
String w = st.nextToken();
if(w.equalsIgnoreCase(var))
total = total+1;
}
if(total > 0)
System.out.println("YES " +total);
else
System.out.println("NO " +total);
}
}
}