How to find the index of the first occurrence of substring in a string in C

1 Answer

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

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

    char *p = strstr(s, needle);

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


    return 0;
}




/*
run:

7

*/

 



answered Feb 1, 2022 by avibootz
edited Feb 1, 2022 by avibootz
...