Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:03:30+00:00 2026-05-27T14:03:30+00:00

I am wondering if it is possible to cut down how many size_t variables

  • 0

I am wondering if it is possible to cut down how many size_t variables I use here. Here is what I have:

std::size_t found, found2, found3, found4 /* etc */;

Each has its own string to find:

found1 = msg.find("string1");
found2 = msg.find("string2");
found3 = msg.find("string3");
found4 = msg.find("string4");
// etc

If the word is found, then it will discard and prevent the message to be shown:

if (found1 != std::string::npos)
{
    SendMsg("You cannot say that word!");
}

I have else if statements until found21. I’d like to cut everything down in size, so it would be clean, but I don’t have a clue how to do it. I would also like it to lowercase the word. I have never used tolower at all either, so I would appreciated it if someone would help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:03:31+00:00Added an answer on May 27, 2026 at 2:03 pm

    To lowercase a string, you can do

    std::transform(msg.begin(), msg.end(), msg.begin(), std::tolower);
    

    Transform takes a begin and end iterator as the first and second arguments, and for each element in that range, applies the fourth argument (a function) and assigns it to what the third iterator is pointing to and increments it. By passing msg.begin() as both the first and third arguments, it will assign the result of the function to what it passed to it. So transform will basically do this:

    for (auto src = begin(msg), dst = begin(msg); src != end(msg); ++src, ++dst)
        *dst = tolower(*src);
    

    but using transform is so much nicer.

    To check whether a string contains any of a list of substrings, you can use a for loop with a vector:

    vector<string> bad_strings { "bad word 1", "bad word 2", "etc" };
    
    for (auto i = begin(bad_strings); i != end(bad_strings); ++i)
        if (msg.find(*i)) {
            SendMsg("You cannot say that word!");
            break; // stop when you find it matches even one bad string
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if its possible to use the Google maps IFRAME code to
I have not worked so much with files: I am wondering about possible issues
I was wondering...is it possible to use a value from an array which is
I was wondering if its possible to have Jira update a ticket's status to
I have the following code (cut down for readability): Main Class: public StartProcess() {
Wondering if its possible to communication from javascript to flash without the use of
I am wondering if it possible to use Android renderscript to manipulate activity windows.
just wondering is it possible to use mixed mode on sql server 2005 for
just wondering if its possible to have a hidden parent and a visible child
I am wondering it is possible to have multiple GridFS in Mongo? The reason

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.