strstr Function in C++: Usage Guide

In C++, the strstr function is used to locate the position of a specified substring within a string. Its prototype is as follows:

char* strstr(const char* str1, const char* str2);

The function strstr will return a pointer to the first occurrence of the substring str2 within the string str1, or nullptr if the substring is not found.

Sample code:

#include <iostream>
#include <cstring>

int main() {
    const char* str1 = "Hello, world!";
    const char* str2 = "world";

    char* result = strstr(str1, str2);

    if (result) {
        std::cout << "Found at position: " << result - str1 << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }

    return 0;
}

The output result is:

Found at position: 7
bannerAds