gpt4 book ai didi

c - 奇怪的 strstr 行为

转载 作者:太空狗 更新时间:2023-10-29 15:19:53 27 4
gpt4 key购买 nike

我正在尝试查找子字符串在字符串中的位置。

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

void find_str(char const* str, char const* substr)
{
char* pos = strstr(str, substr);
if(pos) {
printf("found the string '%s' in '%s' at position: %d\n", substr, str, pos - str);
} else {
printf("the string '%s' was not found in '%s'\n", substr, str);
}
}

int main(int argc, char* argv[])
{
char* str = "gdeasegrfdtyguhıjohhıhugfydsasdrfgthyjkjhghh";
find_str(str, "hh");
find_str("dhenhheme kekekeke hhtttttttttttttttttttttttttttttttttttttttthhtttttttttttt", "hh");

return 0;
}

输出:

found the string 'hh' in 'gdeasegrfdtyguhıjohhıhugfydsasdrfgthyjkjhghh' at position: 19

found the string 'hh' in 'dhenhheme kekekeke hhtttttttttttttttttttttttttttttttttttttttthhtttttttttttt' at position: 4

在第一个例子中它说它在位置 19。但是它不应该说 18 吗?因为在第二个示例中它说 4。它从子字符串的开头开始。

我很困惑。

最佳答案

字符 'ı' 不是 ASCII 字符,因此它可能占用两个字节。

关于c - 奇怪的 strstr 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20759095/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com