【发布时间】:2020-08-18 16:07:55
【问题描述】:
我正在尝试编写一个程序来读取输入(每行有 2 个单词)并且我想打印这些单词。但它给了我一个奇怪的输出。
#include <stdio.h>
int main()
{
char word1[21], word2[21], text[5005];
int line = 0, i;
while (fgets(text, sizeof(text), stdin))
{
sscanf(text, "%s %s", &word1[line], &word2[line]);
line++;
}
for(i = 0; i < line; i++)
{
printf("%s %s", word1, word2);
printf("\n");
}
return 0;
}
当我输入例如:
dog cat
black white
输出是:
dblack cwhite
dblack cwhite
我该如何做这样的输出?
dog cat
black white
【问题讨论】: