String.indexOf function in C

前端 未结 7 1387
Happy的楠姐
Happy的楠姐 2020-11-30 06:41

Is there a C library function that will return the index of a character in a string?

So far, all I\'ve found are functions like strstr that will return the found cha

7条回答
  •  佛祖请我去吃肉
    2020-11-30 07:00

    You can use strstr to accomplish what you want. Example:

    char *a = "Hello World!";
    char *b = strstr(a, "World");
    
    int position = b - a;
    
    printf("the offset is %i\n", position);
    

    This produces the result:

    the offset is 6
    

提交回复
热议问题