C++: Word Censor - Print "Censored

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 7K views
ineedhelpnow
Messages
649
Reaction score
0
Print "Censored" if user text contains the word "darn", else print userInput. end with new line.

Sample program:

Code:
#include 
#include  
using namespace std;

int main() {
   string userInput;

   userInput = "That darn cat.";
   <student code>
    

   return 0;
}

I'm super lost on this. I could definitely use some help/hints. Thanks.
 
Physics news on Phys.org
Read the "Searching and Substrings" section of this page, or, better, read your textbook or lecture notes.
 
we don't have a textbook for the class. he doesn't give us anything to take notes on.
 
ineedhelpnow said:
he doesn't give us anything to take notes on.
Aren't you supposed to bring your own notebooks? Anyway, if you have questions after reading the web page linked above, let us know.
 
lol i meant in his lecture there is nothing useful for us to record down. he goes over the homework and he'll be like ok do this problem and then do this oh and don't do this lesson. and then he'll just talk at as about computer science or computer engineering.
 
So the string contains the word "darn" if and only if the string contains a substring of length 4 equal to "darn". How would you enumerate all substrings of length 4 in a string? How would you check if they are equal to "darn"?

I'm assuming you need to do it manually here, since there are standard functions for doing this sort of thing already.
 
:o I am still stuck on this problem. i really don't understand it all. can someone tell me what command i need to use at least. i might be able to figure the rest out. like do i use: find(), substr()?
 
It is easier to use the function [m]find()[/m] described in the link I gave in post #2. Then checking whether the string is censored is a one-liner. If you were said not to use standard functions, then we need to come up with an algorithm that checks for occurrence of a substring. In that case, you can pretend you have a sequence of letter blocks. Describe how you would check them one at a time (this is important) to find a substring.