-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCensura.java
More file actions
37 lines (29 loc) · 994 Bytes
/
Censura.java
File metadata and controls
37 lines (29 loc) · 994 Bytes
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
import java.util.Scanner;
public class Censura {
public static String censura(String frase,String... indesiderate) {
for(int i=0; i <indesiderate.length; i++)
frase = cancellaStringa(frase,indesiderate[i]);
return frase;
}
public static String cancellaStringa(String frase, String stringa) {
String finale;
int posizione = frase.indexOf(stringa);
while (posizione >= 0) {
finale = frase.substring(posizione + stringa.length());
frase = frase.substring(0, posizione) + finale;
posizione = frase.indexOf(stringa);
}
return frase;
}
public static void main(String[] args) {
System.out.println("Cosa hai mangiato per cena?");
Scanner Tastiera = new Scanner(System.in);
String frase = Tastiera.nextLine();
frase = censura(frase, "scalera" , "patatine fritte", "salato", "birra");
frase = censura(frase, ",");
System.out.println("Saresti più sano se avessi risposto:");
System.out.println(frase);
Tastiera.close();
System.exit(0);
}
}