Bleep.cpp freeform project help

In the references and pointers section in the c++ course, we’re instructed to create a program bleep.cpp. Here is the link to the lesson, and here is the solution.

When trying to plan a way to handle this, I came up with first iterating through the text given with a for loop. The problem I’m having is that if you try to iterate through the given text, which is a std::string, you’ll get letters returned to you. This makes it difficult to compare with the banned word we’re given.
The only thing I can think of is to iterate through the std::string, and if characters “b”, “r”, “o”, “c”, “c”, “o”, “l”, “i” are produced in sequence, then replace them with “*”. But I don’t know the best way to accomplish that, and even if I did, I’m confident there’s a better way to go about this.

If the text were a vector, I’d iterate through that and then that’d return words, which I could then compare with the banned word “broccoli” and push_back() an asterisk or the word depending on whether or not they were equal, however it’s not a vector; it’s a string. Is there a way I can create a vector comprised of words from a string? If so, I’d then be able to iterate through it no problem.

I know formulating a plan is the first step before actually writing the code, so if anyone can look through what I’ve written here and address my questions and overall approach that’d be great. If you have a suggestion as to how you’d do this, I’d greatly appreciate your insight too. I wasn’t really able to deduce the plan from the provided solution.

My solution is different from that you linked to, though the principles are the same.

For the bleep_it function, you can create a string of asterisks that matches the size of the word you want to bleep. That means getting the size of whatever word is sent, then creating your bleep word to match. Notice the solution you linked to did that.

Also notice that step 3 guides you to pass by reference so you can dynamically change the bleep word.

Then use text.find() and text.replace() to put your bleep into the bad word.

There once was a U.S. President who hated ********. It was nothing personal, but even his mother couldn’t get him to eat ******** as he was growing up. He liked baked beans, though. Still he wouldn’t eat ******** even if he could have baked beans too. I like ******** as long as it has enough basalmic vinegar on it. Then ******** tastes okay. The former President, though – he still doesn’t like ********.

Hello “harrjit”, can you tell me how to use text.find() and text.replace()??

can we keep it a string but in the functions file push that text into vector and implement your solution?

could you explain your solution more ?

This is my solution for the Bleep Project, if differs a bit for the Codecademy solution.

Comments would be very appreciate.

Bit late to the party, but a string is a vector actually. It’s a vector in the programming language and you can use for to iterate through it. You just have to use .length instead of .size with the for loop. A code like:

std::string text = "Hello world!"; for (int i = 0; i < text.length(); i++) { // Whatever you want to do with it }

…will iterate through the string just fine, like through any other vector.

A string is not a vector. It’s a totally different class/container. Its implementation uses a series of ptrs to heap allocated data similar to a vector, but it’s not a vector.

It might not be one per se, as I said it, but it behaves like one especially for the purposes of this exercise. I stumbled upon this information (that a string might be a char vector) while reading some other forums and it really stuck with me because it just gave me a different vision of what I can do with strings and how to work with them. It might have been erroneous or I might have remembered it badly, but for this exercise, it helps to think of it as such.

The concept you are describing is iterable. A string provides begin and end iterators thus it is iterable.