【发布时间】:2021-02-23 01:37:03
【问题描述】:
我正在开发一个 C 程序,它将用户输入读取为用空格分隔的字符串。我想要一种方法来读取这些字符串,直到出现空格。我知道 scanf 很容易做到这一点,但我想知道是否可以使用 gets() 或 sscanf() 或 2 的某种组合来做同样的事情。我对 C 非常陌生,只是刚刚开始它,我来自 Java 背景。提前谢谢大家,我真的很感激任何和所有的帮助!!!!在下面找到我到目前为止的代码以及示例输入(***如果未注释 scanf() 部分,则可以实现所需的功能,但目标是仅使用 get() 实现与 scanf() 相同的功能( ) 和 sscanf())。
int main()
{
printf("Enter words seperated by spaces:(. or EOF to stop):\n");
do
{
char s[255];
//scanf("%s",s);
gets(s);
if(strcmp(s,".")==0 || strcmp(s,"EOF")==0)
{
insert_dictionary_order(s);//adding to list
break;
}
else
{
insert_dictionary_order(s);//adding to list
}
}
while(1);
//now printing list
print_list();
return 0;
}
This is a sample text.
The file will be terminated by a single dot: .
The program continues processing the lines because the dot (.)
did not appear at the beginning.
. even though this line starts with a dot, it is not a single dot.
The program stops processing lines right here.
.
You won't be able to feed any more lines to the program.
编辑:答案必须实现 get() 或 sscanf() 或两者。
【问题讨论】: