What is the strstr() function in C++?
The strstr() function takes in two strings (strA and strB) as arguments, finds the first occurrence of strB in strA and returns a pointer to the first character of this occurrence.
1 of 3
Code
The code below implements the function above:
#include <iostream>#include <string.h>using namespace std;int main() {char strA[] = "A red apple vs a green apple";char strB[] = "apple";char* p = strstr(strA, strB); // The strstr() function callstrcpy(p, "strawberry"); // Using the obtained pointer to modify contents of strAcout<< strA;}
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved