OK,
First, you set the pointers up - one for the input string (p), and one for the output string (p3).
while ((p2 = strstr(p, target)) != NULL)
This is to get "p2" to point at the start of the string you want to replace. We do it this way so we can put it in a loop (instead of while ((p2 = strstr(inputstr, target)) != NULL)
But, before you can copy the replacement string to the output string, you have to copy all of the characters from the input string that came *before* the string you want to replace. That's what this complicated-looking piece of code does:
strncpy(p3, p, (strlen(p) - strlen(p2)));
"strncpy" copies a string from (in this case) "p" to "p3", but only for a number of characters specified in the 3rd parameter. The third parm in this case is the length of the input string (p) minus the length of the string from the match position. The difference is the length of the string *before* the match position. We copy that to the output string, then push the pointer to the output string up by that same amount.
Now, we copy the replacement string to the output, and push the pointer up by the length of the replacement string:
// Copy the replacement string to the result
strcpy(p3, replacestr);
// Increment the result pointer
p3 += strlen(replacestr);
Now, we move up the pointer in the input string to the match location …