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.
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