如何使用strlen()通过do while循环从C中的用户扫描字符串?

如何使用strlen()通过do while循环从C中的用户扫描字符串?,c,C,下面的代码不起作用,它不会返回我键入的单词,事实上,它从未停止请求字符串。你能给我解释一下如何从用户那里得到一个字符串吗 #include <stdio.h> #include <string.h> int main (void) { char word[20]; do { scanf("%c", word); } while (strlen (word) > 0 && strlen (wor

下面的代码不起作用,它不会返回我键入的单词,事实上,它从未停止请求字符串。你能给我解释一下如何从用户那里得到一个字符串吗

#include <stdio.h>
#include <string.h>

int main (void)
{
    char word[20];

    do
    {
       scanf("%c", word);
    }
    while (strlen (word) > 0 && strlen (word) <20);

    printf("the word is %s", word);
}
#包括
#包括
内部主(空)
{
字符字[20];
做
{
scanf(“%c”,单词);
}
while(strlen(word)>0&&strlen(word)
word
不是c字符串,因为它没有“NUL”终止符

执行
strlen
可能会调用,因为
word
没有
\0

你能给我解释一下如何从用户那里得到一个字符串吗

#include <stdio.h>
#include <string.h>

int main (void)
{
    char word[20];

    do
    {
       scanf("%c", word);
    }
    while (strlen (word) > 0 && strlen (word) <20);

    printf("the word is %s", word);
}
一种方法是使用
fgets
函数轻松读取字符串。

%c“
=>
%s”
请读取手册。“c匹配长度由最大字段宽度(默认值1)指定的字符序列;下一个指针必须是指向char的指针,并且必须有足够的空间容纳所有字符(未添加终止的空字节)。通常的前导空格跳过被抑制。若要首先跳过空格,请使用格式为“@Broman
%s”
=>
%19s”