Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

strstr()
c++

What is the strstr() function in C++?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 call
strcpy(p, "strawberry"); // Using the obtained pointer to modify contents of strA
cout<< strA;
}
Note: anything after the pointer (and not just its first occurrence) is replaced by the new string.

RELATED TAGS

strstr()
c++
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring