Hello guys, how can I solve the following problem:

I made a pointer that receives the position found by the function srtstr

char * pon;
pon = strstr (string1, string2);

The problem is now, I need to know the position where the string 2 in string1 within the whole.
anyone have any tips?

Why do I need this integer value to use it in a "for".

3 Years
Discussion Span
Last Post by Ancient Dragon

What precisely are you trying to accomplish? FWIW, strstr() returns a pointer to where in string1 it found string2, and returns null (0) if not found.


If you want to just search for string2 as a whole word then after getting the pointer from strstr() check the character just before the pointer and the character just following the end of the word in string2. For example is string2 = "Hello" then check the character immediately before the 'H' and the character after the 'o'. Hint: you do not need a loop to do these checks.

size_t j = pon - string1[0];

is the index to the first char of string2, within string1.


As usual, AD humbles us all! :-)
FWIW, I'm just an OD (Old Dragon) - hit 65 earlier this year!

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.