试图突破 fgets while 循环

Trying to break out of fgets while loop

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

int main(){
    char c[20], result[50];
    int bool = 0, count = 0, i;
    
    while(fgets(c,20,stdin) != NULL){
        int stringSize = strlen(c);
        if(stringSize == 11){
            
            int ascii = (int)(c[i]);
            
            for(i = 0; i < stringSize; i++){
            
                if(ascii >= 'A' && ascii <= 'Z'){
                    bool = 1;
                }
            }
        }
    }
        if(bool == 1){
            count++;
            strcat(result,c);
        }
    
    printf("%d", count);
    printf("%s",result);
}

早上好,我对编程还很陌生,我已经花了很长时间在谷歌上搜索和搜索这个问题,但我似乎无法全神贯注。 基本上我试图过滤一个 fgets 以便它读取每个字符串,如果它们是大写字母,它们就是“有效的”。但是,我什至无法让 fgets 停止接受更多输入。

编辑:想法是将每个具有 10 个大写字母的字符串存储在结果中,并且一旦用户未提供输入 ('\0')

,fgets while 循环就会中断

如果您从标准输入流输入字符串,那么最好按以下方式重写 while 循环的条件

while( fgets(c,20,stdin) != NULL && c[0] != '\n' ){

在这种情况下,如果用户只是按下 Enter 键而没有输入字符串,那么循环将停止迭代。

注意fgets可以在输入的字符串后面追加换行符'\n'。你应该删除它

c[ strcspn( c, "\n" ) ] = '[=11=]';

然后你可以写

size_t n = strlen( c );

if ( n == 10 )
{
    size_t i = 0;
    while ( i != n && 'A' <= c[i] && c[i] <= 'Z' ) ++i;

    bool = i == 10;
}

注意使用名字bool是个坏主意,因为这样的名字在header <stdbool.h>.

中作为宏被引入

好像还有这个if语句

    if(bool == 1){
        count++;
        strcat(result,c);
    }

必须在 while 循环中。并且数组结果必须初始化

char c[20], result[50] = { '[=14=]' };