解决方案

我们可以使用strstr完成所需的操作。 strstr返回一个指向找到的字符的指针,因此我们可以使用指针算术,
如果未找到任何字符串,strchr()将返回NULL

char *a = "Hello World!";
char *b = strstr(a, "World");

int position = b ? b - a : -1;

printf("the offset is %i\n", position);

产生结果:

the offset is 6
C语言 String.indexOf函数

在C语言中,如何返回字符在原始字符串中的位置?
在C语言中,有什么函数可以返回字符串中字符的索引?

日期:2020-03-24 22:08:25 来源:oir作者:oir