-
Notifications
You must be signed in to change notification settings - Fork 0
/
Censorship.java
59 lines (50 loc) · 1.15 KB
/
Censorship.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Name :
* Matric. No :
* PLab Acct. :
*/
import java.util.*;
class Censorship {
private void run() {
Scanner sc = new Scanner(System.in);
// num is the number of censored words
int num = sc.nextInt();
// declare an array list of censored words
ArrayList<String> words = new ArrayList<String>();
for (int i = 0; i < num; i++) {
String word = sc.next();
words.add(word);
}
// check if all other words should be censored and print the result
int count = 0;
while (sc.hasNext()) {
String str = sc.next();
// test whether str matches any word in words
boolean flag = false;
for (String word : words) {
word = word.toLowerCase();
if (word.equals(str.toLowerCase())) {
flag = true;
}
}
if (count != 0) {
System.out.print(" ");
}
if (flag == true) {
// str be censored
for (int i = 0; i < str.length(); i++) {
System.out.print("*");
}
} else {
// str not censored
System.out.print(str);
}
count++;
}
System.out.println();
}
public static void main(String[] args) {
Censorship newCensorship = new Censorship();
newCensorship.run();
}
}