Answer to Question #99436 in C++ for Caleb

Question #99436
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.
#include <iostream>
#include <string>
using namespace std;

int main() {
string userInput;

getline(cin, userInput);

/* Your solution goes here */

return 0;
}
1
Expert's answer
2019-12-01T13:03:20-0500
#include <iostream> 
#include <string> 

using namespace std; 

int main() { 
  string userInput; 
  getline(cin, userInput); 
  /* Your solution goes here */ 
  for (int i = 0; i + 4 <= userInput.size(); i++) {
    if (userInput.substr(i, 4) == "darn") {
      cout << "Censored\n";
      return 0;
    }
  }
  cout << userInput << endl;

  return 0; 
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS