. Advertisement .
..3..
. Advertisement .
..4..
......... ADVERTISEMENT ......... ..8..
Print ‘Censored if userinput contains the word”darn’, else print userinput End with newline. Ex: if userinput is “That darn cat.:. then output is:
Censored
Ex: If userinput is Dang, that was scaryl”, then output is:
Dang, that was scary
Note: If the submitted code has an out-of-range access, the system will stop running the code after a few seconds, and report Program end never reached The system doesn’t print the test case that caused the reported message.
1 import java.util.Scanner;
2
3 public class Censoredwords {
4 public static void main (String [] args) {
5 Scanner scnr = new Scanner(System.in);
6 String userInput;
7
8 userInput = scnr.nextline( );
9
10 / * Your solution goes here * /
11
12 }
13 }]
This method “indexOf()” within Java is often used when search for a specific substring or character within the string. It returns an index of the substring or character if it’s within the string.
Below is the code:
CensoredWords.java
Package classes1
Import java.util.Scanner
Public class CensoredWords {
public static void main(String[] args) {
Scanner cnr = new scanner(System.in);
String userInput;
userInput = “scnr.nextLine()
if(userInput.contains(“darn”)){
System.out.println(“Censored”);
}else{
System.out.println(userInput);
}
}
}
output: