Check If a String Contains a Substring in C++



A substring is a continues sequence of characters within a larger string. For example, the string "point" a substring of the string "TutorialsPoint". In this article, we will learn different approaches to check if a string contains a substring in C++.

Here is a list of approaches for checking if a string contains a substring, which we will be discussing in this article with stepwise explanation and complete example codes.

Checking Substring Using find()

The find() function is a member function of the string class in C++. It returns the position of the first occurrence of a substring in a string. If the substring is not found, it returns npos, which is a special value that represents the end of the string.

Example

In the code below, we define two strings, text and keyword. The find() function is then used to check if the keyword is present in the text. If the keyword is found, we print a message saying that the substring is found. Otherwise, we print a message saying that the substring is not found.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string text = "Hello, world!";
    string keyword = "world";

    if (text.find(keyword) != string::npos) {
        cout << "Substring found!" << endl;
    }
    else {
        cout << "Substring not found!" << endl;
    }
}

The output of the above code will be:

Substring found!

Checking Substring Using search()

The search() function is another member function of the algorithm library in C++. It performs binary search and returns the position of the first occurrence of a value in the range. This is more generic algorithm and it will even work on iterators. let see an example of how to use it.

Example

In the code below, we used iterators to run a binary search on the text string. If the substring is found, we print a message saying that the substring is found. Otherwise, we print a message saying that the substring is not found.

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
    string text = "Hello, world!";
    string keyword = "war";

    auto it = search(text.begin(), text.end(), keyword.begin(), keyword.end());

    if (it != text.end()) {
        cout << "Substring found!" << endl;
    }
    else {
        cout << "Substring not found!" << endl;
    }
}

The output of the above code will be:

Substring not found!

Checking Substring Using regex_search()

The regex_search() function is a member function of the regex library in C++. It performs a regular expression search and returns a boolean value indicating whether a match was found or not. Let's see an example of how to use it.

Example

In the code below, we used the regex_search() function to check if the text string contains the keyword string.

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
    string text = "Hello, world!";
    string keyword = "wor-ld";

    auto it = search(text.begin(), text.end(), keyword.begin(), keyword.end());

    if (it != text.end()) {
        cout << "Substring found!" << endl;
    }
    else{
        cout << "Substring not found!" << endl;
    }
}

The output of the above code will be:

Substring not found!

Checking Substring Manually

This approach works only for c style strings, where we can use the strstr() function to check if a substring is present in a string. Let's see an example of how to use it.

Example

In the code below, we use the strstr() function to check if the keyword string is present in the text string.

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    const char* text = "Hello, world!";
    const char* keyword = "world";

    if (strstr(text, keyword) != nullptr) {
        cout << "Substring found!" << endl;
    }
    else {
        cout << "Substring not found!" << endl;
    }
}

The output of the above code will be:

Substring found!
Updated on: 2025-04-17T18:56:43+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements