How to get the index of second occurrence of substring in a string with C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

int main() {
    const char s[] = "c java c++ java c++ php c";
    const char needle[] = "c++";

    char *p = strstr(s, needle);

    p = strstr(s + ((p - s) + strlen(needle)), needle);

    printf("%d", p - s);


    return 0;
}




/*
run:

16

*/

 



answered Feb 1, 2022 by avibootz
...