In C++, std::strstr() is a predefined function used for string handling. string.h is the header file required for string functions.
This function takes two strings s1 and s2 as an argument and finds the first occurrence of the sub-string s2 in the string s1. The process of matching does not include the terminating null-characters(‘\0’), but function stops there.
char *strstr (const char *s1, const char *s2); Parameters: s1: This is the main string to be examined. s2: This is the sub-string to be searched in s1 string.
Return Value: This function returns a pointer points to the first character of the found s2 in s1 otherwise a null pointer if s2 is not present in s1. If s2 points to an empty string, s1 is returned.
String found First occurrence of string 'for' in 'GeeksforGeeks' is 'forGeeks'
Application : Replace a string with another
In this example with the help of strstr() function we first searches for the occurrence of STL sub-string in s1 and after that replaces that word with Strings.
Fun with Strings
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- basic_istream::readsome() in C++ with Examples
- std::is_function template in C++ with Examples
- type_traits::is_null_pointer in C++
- std::extent() template in C++ with Examples
- basic_istream::swap() in C++ with Examples
- basic_istream::peek() in C++ with Examples
- basic_istream::unget() in C++ with Examples
- exception::bad_exception in C++ with Examples
- std::bad_array_new_length class in C++ with Examples
- basic_istream::seekg() in C++ with Examples
- functional::bad_function_call in C++ with Examples
- basic_istream::get() in C++ with Examples
- typeinfo::bad_cast in C++ with Examples
- basic_istream::putback() in C++ with Examples
Improved By : nidhi_biet