【发布时间】:2015-01-09 23:00:22
【问题描述】:
我正在研究一个使用strstr() 函数的示例。
如果我输入“Pamela Sue Smith”,为什么程序输出““Pamela”是一个子字符串!”而不是““Pamela Sue Smith”是一个子字符串!”。
#include <stdio.h>
#include <string.h>
void main(void)
{
char str[72];
char target[] = "Pamela Sue Smith";
printf("Enter your string: ");
scanf("%s", str);
if (strstr( target, str) != NULL )
printf(" %s is a sub-string!\n", str);
}
【问题讨论】:
-
%s只读一个字。 -
如果你想读一整行,使用
fgets(),而不是scanf()。 -
你把你对
strstr的论据交换了。 -
@Barmar:不完全是,不是。虽然那是一部分。