90.0k views
4 votes
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 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 Answer

6 votes

Hi, you haven't provided the programing language in which you need the code, I'll just explain how to do it using Python, and you can apply the same method for any programming language, pseudocode or flowchart.

Answer:

1 userInput = input("Please enter 'userInput':\\")

2 if "darn" in userInput.lower():

3 print("Censored")

4 else:

5 print(userInput\\)

Explanation line by line:

  • Line one asked for the user input, and store the value in a variable called userInput. Inside the input function, you put some text to indicate to the user to enter something and \\ is the new line character.
  • Line two checked if the word "darn" is in the user input, for this, we use the built-in function in. Because we don't know if the user inputs the word "darn" in upper case, lower case or a mix of both we use the method .lower() to change the user input to lowercase and make the validation (Python is case sensitive -> A is not equal to a).
  • Line three prints the word "Censored" if and only if the word "darn" is in userInput.
  • Line fourth allows going to line five if and only if the word "darn" is NOT in userInput.
  • Finally, line five prints the word entered by the user if and only if the word "darn" in NOT in userInput.

User Wablab
by
5.2k points