Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ I cannot get my program to block words properly

rastafari

New Coder
My program blocks words when they are separate but when you add anything it does not censor it. Example ----delta--- should be blocked but does not. Not sure where the problem is, I'm very new to c++


C++:
int main()
{
    string bad[] = {"alpha", "beta", "gamma", "delta"};
    set<string> bad_words(bad, bad + sizeof(bad) / sizeof(string));

    string input;
    while(cin >> input)
    {
         if (bad_words.find(input)!=bad_words.end()){
            cout << "CENSORED ";
        }
    else {
            cout << input << " ";
        }
    }
}
 
Last edited by a moderator:
The find function obviously only finds a word when it's on its own, not when it is embedded in another word. You'd need another approach to do this.
What I would do is take the easy way out. Make a copy of the input string, remove all non-alphabetic characters from it, and use that in the lookup.
This is assuming that all the "bad" words are purely alphabetic, which may or may not be the case.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom