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

1 Answer

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

int main() {
    char s[64] = "c python c++ java c++ php c++";
    char *p = s;

    while( (p = strstr(p + 1, "c++")) ) {
        printf("%d ", p - s);
    }

    return 0;
}




/*
run:

9 18 26

*/

 



answered Feb 2, 2022 by avibootz
edited Feb 16, 2022 by avibootz
...