72.0k views
3 votes
Print "Censored' if userlnput contains the word "darn, else print userlnput. End with newline. Ex: If userinput is "That darn cat., then output is: Censored Ex: If userlnput is "Dang, that was scary!", 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; 3 public class CensoredWords 4public static void main ( 1 test passed String args) Scanner scnr -new Scanner(System.in); String userInput; A tests passed userInput scnr.nextLine); 10 Your solution goes here 12 13 Run

1 Answer

6 votes

Answer:

The code is given below in Java with appropriate comments

Step-by-step explanation:

//Import the input.

import java.util.Scanner;

class CensoredWords

{

//Define the main method.

public static void main(String args[ ])

{

//Define the variables.

String userInput="" ;

//Define the scanner object

Scanner scobj = new Scanner(System.in);

//Accept the userInput.

System.out.print("Enter String: ");

userInput=scobj.nextLine();

//Check if the input contains darn.

//Print censored.

if(userInput.toUpperCase().indexOf("DARN") != -1)

System.out.printf("Censored");

//IF the input does not contains darn

//Print userInput.

else

System.out.printf(userInput)

return;

}

}

User Rais Alam
by
4.3k points