C++ – Check if string contains a character

In this article, we will learn about three different ways to check if a string contains a character or not.

Check if a string contains a character using string::find()

In C++, the string class provides different overloaded versions of function find() to search for sub-strings or characters in the string object. In one of the overloaded versions, the find() function accepts a character as an argument and returns the character’s position in the string. If the character doesn’t exist in the string, then it returns string::npos. Let’s use this to check if a string contains the given character. 

Example 1:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sample = "Some sample text";
    
    char ch = 't';

    // Check if string contains the character or not
    if (sample.find(ch) != string::npos )
    {
        cout<<"Yes, string contains the character - "<< ch << endl;
    }
    else
    {
        cout<<"No, string do not contains the character - "<< ch << endl;
    }

    return 0;
}

Output:

Yes, string contains the character - t

In the above example, we looked for the character ‘t’ in the string. As the given string contains the character ‘t’, therefore it printed ‘Yes’. Now let’s check out a negative example,

Example 2:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sample = "Some sample text";
    
    char ch = 'z';

    // Check if string contains the character or not
    if (sample.find(ch) != string::npos )
    {
        cout<<"Yes, string contains the character - "<< ch << endl;
    }
    else
    {
        cout<<"No, string do not contains the character - "<< ch << endl;
    }

    return 0;
}

Output:

No, string do not contains the character - z

In the above example, we looked for the character ‘z’ in the string. As the given string do not have the character ‘z’, therefore it printed ‘No’.

Check if a string contains a character using std::find() – STL Algorithm

We can use the STL Algorithm std::find() to check if a given string contains a character or not. The std::find() function accepts a range and a value as arguments. Then it iterates over all the elements in the range and looks for the given value. If an element exists in the range, then it returns the iterator of that element; otherwise, it returns the iterator pointing to the end of the range. We can use this to check if a string contains a character or not. For example,

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string sample = "Some sample text";
    
    char ch = 't';

    // Check if string contains the character or not
    bool result = find(sample.begin(), sample.end(), ch ) != sample.end();

    if (result)
    {
        cout<<"Yes, string contains the character - "<< ch << endl;
    }
    else
    {
        cout<<"No, string do not contains the character - "<< ch << endl;
    }

    return 0;
}

Output:

Yes, string contains the character - t

A string is a range of characters. Therefore we passed the iterators pointing to the beginning and end of the string, along with character ‘t’ as arguments to find() function. It returned the iterator pointing to the position of character ‘t’ in the string. If the given character doesn’t exist in the string, then it returns the string::end().

Check if string contains the character using for-loop

We can also iterate over all characters of string using a for loop and check if given character exist in the string or not.

For example,

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sample = "Some sample text";
    
    char ch = 't';

    
    bool result = false;
    // Check if string contains the character or not
    for(auto & elem: sample)
    {
        if (elem == ch)
        {
            result = true;
            break;
        }
    }

    if (result)
    {
        cout<<"Yes, string contains the character - "<< ch << endl;
    }
    else
    {
        cout<<"No, string do not contains the character - "<< ch << endl;
    }

    return 0;
}

Output:

Yes, string contains the character - t

Summary:

We learned about three different ways to check if a string contains a character or not.

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