在 C 编程中,如何将用户输入存储在变量中以便可以从中获取子字符串? 在控制台中输入“hello Point”时出现错误:子字符串为 NULL。这意味着我的word变量是空的?我到底做错了什么以及为什么?
#include <stdio.h>
#include <string.h>
int main()
{
char word[100];
printf ("Enter a word: ");
scanf ("%s", word);
const char needle[] = "Point";
char *ret;
ret = strstr(word, needle);
printf("The substring is: %s\n", ret);
return(0);
}
最佳答案
%s
for scanf()
在发现空格时停止读取。
尝试使用 scanf("%99[^\n]", word);
代替。 (添加了 99
以避免缓冲区溢出)
关于c - 将用户输入存储在变量中,以便我可以从中获取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34293316/