[英]Looking for better understanding of strstr function in C
I'm trying to use the strstr function in C for a bigger project, and couldn't get it to work, so I formed a small test file to try and learn it better, only problem are results are not what I expected. 我试图将C中的strstr函数用于更大的项目,但无法使其正常工作,因此我形成了一个小的测试文件以尝试更好地学习它,唯一的问题是结果不是我期望的。 Can someone please explain to me based on this c file I have, what strstr should return for me, and how I am using it wrong?
有人可以根据我拥有的C文件向我解释一下,应该为我返回什么strstr,以及我使用它的方式有误吗? When i run this program it is returning NULL for all uses of strstr, what I expect is that it returns NULL for the first 2, but for one of the second two (I know not both) it should print the string "brush".
当我运行该程序时,所有strstr使用都返回NULL,我期望的是前两个返回NULL,但对于后两个(我不都知道),它应该打印字符串“ brush”。 What am I doing wrong or expecting wrong?
我在做什么错或期望错了?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char str1[11]="toothpaste";
char str2[11]="toothbrush";
char str3[6]="brush";
char str4[6]="paste";
printf("\n\nstr1=%s",str1);
printf("\nstr2=%s",str2);
printf("\nstr3=%s",str3);
printf("\nstr4=%s",str4);
printf("\n\nResult of strstr(1,2) is %s",strstr(str1,str2));
printf("\nResult of strstr(2,1) is %s",strstr(str2,str1));
printf("\nResults of strstr(2,3) is %s",strstr(str1,str3));
printf("\nResults of strstr(3,2) is %s\n\n",strstr(str3,str1));
return 0;
}
The last two cases call strstr
on str1
and str3
( not str2
). 最后两种情况在
str1
和str3
上调用strstr
( 不是 str2
)。 Thus they're looking for "brush"
in "toothpaste"
and vice versa. 因此,他们正在
"toothpaste"
寻找"brush"
,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.