Sebastian
Sebastian

Reputation: 972

Search a string with strstr in a part of an char *

currently I search a char * with strstr but I dont want to search in the complete one, only from the 42. char to the end. How can I achieve this?

Upvotes: 0

Views: 642

Answers (2)

umi
umi

Reputation: 3892

Add 42 to the original str pointer (beware that original string length is greater than 42 though):

first_occurence = strstr(str + 42, substr);

Upvotes: 1

unwind
unwind

Reputation: 399949

Just provide strstr() with a pointer to the first character to include in the seach:

if(strstr(the_haystack + 42, "the needle") != NULL)
  printf("found needle at end of haystack!\n");

Of course, this assumes that the_haystack really is at least 42 characters long.

Upvotes: 2

Related Questions