Check if a string contains certain characters in C++

In this article, we will discuss different ways to check if a string contains certain characters or not.

Suppose we have a sequence of characters like,

vector vecOfChars{'s', 'a', 'i'};

We want to check if a given string contains all of these characters or not. Let’s see how to do this,

Check if a string contains certain characters using STL function all_of()

In C++, the Standard Template Library provides a function all_of(). It checks that a given predicate returns true for all the sequence elements. As a sequence, we can pass characters in vector and as a predicate pass a lambda function that checks if the character exists in the string. For example,

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

/*
    Returns true if given string contains all
    the given characters, otherwise returns false.
*/
bool contains_chars(const string & mainStr,
                    const vector<char> & vecOfChars)
{
    return all_of(  vecOfChars.begin(),
                    vecOfChars.end(),
                    [&](const char & ch){
                        return (mainStr.find(ch) != string::npos);
                    });
}

int main()
{
    string sample_str = "This is a Sample string";
    vector<char> vecOfChars{'s', 'a', 'i'};

    cout<< contains_chars(sample_str, vecOfChars) << endl;
    cout<< contains_chars(sample_str, {'k'}) <<endl;
    cout<< contains_chars(sample_str, {'l', 'h'}) <<endl;
    cout<< contains_chars(sample_str, {'l', 'z'}) <<endl;
    cout<< contains_chars("", vecOfChars) <<endl;


    return 0;
}

Output:

1
0
1
0
0

Analysis if the Output:

  • String “This is a Sample string” has characters ‘s’, ‘a’ and ‘i’.
  • String “This is a Sample string” does not have the character ‘k’.
  • String “This is a Sample string” has characters ‘l’ and ‘h’.
  • String “This is a Sample string” does not have the character ‘l’ and ‘z’.
  • For empty string, it returns false.

Check if a string contains certain characters using for loop

Iterate over all the characters in the vector. During iteration, for each character, check if it exists in the given string or not. As soon as we encounter a character that does not exist in the string, stop the iteration because it means the string does not have all of the specified characters. We have created a separate function for this. Let’s see some examples,

#include <iostream>
#include <vector>

using namespace std;

/*
    Returns true if given string contains all
    the given characters, otherwise returns false.
*/
bool contains_chars(const string & mainStr,
                    const vector<char> & vecOfChars)
{
    bool result = !mainStr.empty();
    // Iterate over all characters in vector
    for (const char & ch : vecOfChars)
    {
        // Check if character exist in string
        if(mainStr.find(ch) == string::npos)
        {
            result = false;
            break;
        }
    }
    return result;
}

int main()
{
    string sample_str = "This is a Sample string";
    vector<char> vecOfChars{'s', 'a', 'i'};

    cout<< contains_chars(sample_str, vecOfChars) << endl;
    cout<< contains_chars(sample_str, {'k'}) <<endl;
    cout<< contains_chars(sample_str, {'l', 'h'}) <<endl;
    cout<< contains_chars(sample_str, {'l', 'z'}) <<endl;
    cout<< contains_chars("", vecOfChars) <<endl;


    return 0;
}

Output:

1
0
1
0
0

Analysis of the Output:

  • String “This is a Sample string” has characters ‘s’, ‘a’, and ‘i’.
  • String “This is a Sample string” does not have the character ‘k’.
  • String “This is a Sample string” has characters ‘l’ and ‘h’.
  • String “This is a Sample string” does not have the character ‘l’ and ‘z’.
  • For empty string, it returns false.

Check if a string contains certain characters using Regex

Iterate over all the characters in the vector, and for each character, check if it exists in the given string or not using Regex. For example,

#include <iostream>
#include <vector>
#include <regex>

using namespace std;

/*
    Returns true if given string contains all
    the given characters, otherwise returns false.
*/
bool contains_chars(const string & mainStr,
                    const vector<char> & vecOfChars)
{
    bool result = !mainStr.empty();
    for (const char & ch: vecOfChars)
    {
        std::regex pattern(".*" + string(1, ch) + ".*");
        result = result && regex_match(mainStr, pattern);
        if( !result)
        {
            break;
        }
    }
    return result;
}

int main()
{
    string sample_str = "This is a Sample string";
    vector<char> vecOfChars{'s', 'a', 'i'};

    cout<< contains_chars(sample_str, vecOfChars) << endl;
    cout<< contains_chars(sample_str, {'k'}) <<endl;
    cout<< contains_chars(sample_str, {'l', 'h'}) <<endl;
    cout<< contains_chars(sample_str, {'l', 'z'}) <<endl;
    cout<< contains_chars("", vecOfChars) <<endl;


    return 0;
}

Output:

1
0
1
0
0

Analysis of the Output:

  • String “This is a Sample string” has characters ‘s’, ‘a’, and ‘i’.
  • String “This is a Sample string” does not have the character ‘k’.
  • String “This is a Sample string” has characters ‘l’ and ‘h’.
  • String “This is a Sample string” does not have the character ‘l’ and ‘z’.
  • For empty string, it returns false.

Summary:

We learned about three different ways to check if a string contains certain characters or not in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top