A program to censor unwanted words Problem:

Censor Unwanted words.

Solution:

Note: I am beginner and self learning programming by reading the book "Programming: Principles and Practises" by Bjarne Stroustrup and this a "try this" exercise from chapter 4. 

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

int main()
{
    std::vector&lt;std::string&gt; words;

    std::cout &lt;&lt; "Enter a list of words.\n";
    std::string temp;

    while(std::cin&gt;&gt;temp){
        words.push_back(temp);
        }

    for(auto w : words){
        if(w=="idiot"||w=="nonsense"||w=="hyper"){
            std::cout&lt;&lt;"BlEEP"&lt;&lt;" ";
        }
        else std::cout&lt;&lt;w&lt;&lt;" ";
    }
}


Sample Test run.
That's fine. You have exactly the includes you need, you didn't use using namespace, and you've used std::cin correctly.

However, I'd recommend you to use a code formatter next time, since some for your closing braces are misaligned. And if you use braces around a single statement in if, do the same for else.

Other than that, you can take a const auto &amp; word instead of auto w to remove copies. Also word is a nicer name than w. Try to be more verbose, it helps when you read the code a second time.

Since we do not use temp after the while, should keep its scope smaller. We can use a for loop, wrap braces around the while and temp's declaration (which does the same), or write a small (inline) function.

We end up with

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

int main() {
    std::vector&lt;std::string&gt; words;

    std::cout &lt;&lt; "Enter a list of words.\n";

    for(std::string temp; std::cin &gt;&gt; temp;){
        words.push_back(temp);
    }

    for(const auto &amp;word : words){
        if(word == "idiot" || word == "nonsense" || word == "hyper"){
            std::cout &lt;&lt; "BlEEP" &lt;&lt;" ";
        } else {
            std::cout &lt;&lt; word &lt;&lt; " ";
        }
    }
}


I've added some whitespace to make the code slightly easier to read. However, our code works well for three words, but what about four, five or even ten? As soon as we have more words than fit in a single line, we probably want to store them in a container.

Exercise

Read the banned words from a file. Use those words to filter the text just like you did.