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

Recommended Answers

All 7 Replies

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.

size_t j = pon - &string1[0];

you forgot to make string1[0] a pointer.

Yes I did, thanks AD.

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

As usual, AD humbles us all! :-)

Nope -- that honor goes to Mike.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.