How do I find the index of a character within a string in C?

前端 未结 5 1209
野的像风
野的像风 2021-01-31 15:12

Suppose I have a string \"qwerty\" and I wish to find the index position of the e character in it. (In this case the index would be 2)

5条回答
  •  情歌与酒
    2021-01-31 15:47

    What about:

    char *string = "qwerty";
    char *e = string;
    int idx = 0;
    while (*e++ != 'e') idx++;
    

    copying to e to preserve the original string, I suppose if you don't care you could just operate over *string

提交回复
热议问题