C++ Cstring Strstr()

The strstr() function in C++ cstring strstr()  is part of the C-string library and is used to locate the first occurrence of a substring in a larger string. It returns a pointer to the first character of the substring if found, or nullptr if the substring is not found.

Syntax:

char* strstr(const char* haystack, const char* needle);

Parameters:

haystack: The string to be searched.

needle: The substring to search for within haystack.

Example Usage:

#include

#include

int main() {

    const char* text = "Hello, welcome to the world of C++ programming.";

    const char* search = "C++";

    char* result = strstr(text, search);

    if (result != nullptr) {

        std::cout << "Substring found at: " << result << std::endl;

    } else {

        std::cout << "Substring not found." << std::endl;

    }

    return 0;

}

Key Points:

strstr() is case-sensitive.

Returns a pointer to the first occurrence of the substring or nullptr if not found.