C语言,统计输入正文中每个数字字符,英文字符和其他字符出现的次数,以EOF结束

#include
int main()
{
char c;
int i,x[]={0,0,0,0,0,0,0,0,0,0,0,0};
for(;;){
c=getchar();
if(c=='0')x[0]+=1;
else if(c=='1')x[1]+=1;
else if(c=='2')x[2]+=1;
else if(c=='3')x[3]+=1;
else if(c=='4')x[4]+=1;
else if(c=='5')x[5]+=1;
else if(c=='6')x[6]+=1;
else if(c=='7')x[7]+=1;
else if(c=='8')x[8]+=1;
else if(c=='9')x[9]+=1;
else if((c<='z'&&c>='a')||(c<='Z'&&c>='A'))x[10]+=1;
else if(c==26)break;
else x[11]+=1;
}
for(i=0;i<10;i++)
printf("Number %d: %d\n",i,x[i]);
printf("characters: %d\n",x[10]);
printf("other: %d",x[11]);
return 0;
}
这个程序我按完换行,在输入^Z不输出,^Z只有跟着其他字符才有输出,为什么?

如果前面有字符,Ctrl+Z视为一行结束,返回26。
如果前面没有字符,第一个字符就是Ctrl+Z,视为EOF,返回-1。

else if(c==26)break; 改为:else if(c==26 or c==-1)break;
另外,你那个输出的循环,少了一个{},不知道是否是你复制粘贴的原因

着代码结构也太差劲了

 #include <stdio.h>
int main()
{
    char c;
    int i, x[11] = { 0 };
    for (;;){
        c = getchar();
        //if (c == '0')x[0] += 1;
        //else if (c == '1')x[1] += 1;
        //else if (c == '2')x[2] += 1;
        //else if (c == '3')x[3] += 1;
        //else if (c == '4')x[4] += 1;
        //else if (c == '5')x[5] += 1;
        //else if (c == '6')x[6] += 1;
        //else if (c == '7')x[7] += 1;
        //else if (c == '8')x[8] += 1;
        //else if (c == '9')x[9] += 1;

        int d = c - '0';
        if (d >= 0 && d <= 9)
        {
            x[d] += 1;
        }

        else if ((c <= 'z'&&c >= 'a') || (c <= 'Z'&&c >= 'A'))x[10] += 1;
        else if (c == EOF)break;
        else x[11] += 1;
    }
    for (i = 0; i<10; i++)
        printf("Number %d: %d\n", i, x[i]);
    printf("characters: %d\n", x[10]);
    printf("other: %d", x[11]);
    return 0;
}

这里,我用Ctrl+C表示结束。