在输入中检测白色 space

Detect white space in the input

我想制作一个只接受用户小写字符的程序。 我希望它在输入中有空格、大写字母或字母表旁边的任何字符时打印错误。

但是我的代码运行异常,我不确定为什么。 仅当空格或大写字母是输入中输入的第一个字符时,才会打印错误消息。即使我正在使用 fgetc 查找空格来扫描整个字符串,这怎么可能?

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

int main ( void )
{
    char buff[BUFSIZ];
    char ch = fgetc(stdin);
    if  (fgets( buff, sizeof buff, stdin ) != NULL && islower(ch)) {
        while (ch != ' ' && ch != EOF)
        {
            printf("There are No Spaces in the input!\n");
            return 0;
        }
    }
    printf("Error\n");
}

您没有扫描整个字符串。您将第一个字符放入 ch,然后将该行的其余部分放入 buff(或多或少),然后如果 ch 是小写字符,您的程序将反复打印 "There are no spaces"结束因为你再也不会改变 ch.