You can use this simple modification of strpos
#include <stdio.h> #include <stdlib.h> #include <string.h> int strpos(char *haystack, char *needle, int offset); int main() { char *p = "Hello there all y'al, hope that you are all well"; int pos = strpos(p, "all", 0); printf("First all at : %d\n", pos); pos = strpos(p, "all", 10); printf("Second all at : %d\n", pos); } int strpos(char *hay, char *needle, int offset) { char haystack[strlen(hay)]; strncpy(haystack, hay+offset, strlen(hay)-offset); char *p = strstr(haystack, needle); if (p) return p - haystack+offset; return -1; }
Howard j
source share