输入15个字符,统计并输出空格或回车、数字字符和其他字符的个数。要求使用switch语句编写。请注意,输入15个字符后,需回车表示输入结束,这最后一个回车表示输入结束,不统计在内。

#include <stdio.h>
int main()
{
int blank,digit,i,other;
char ch;
blank=digit=other=0;
for(i=1; i<=15; i++){
ch=getchar();
switch(ch)
{
case ' ':case '\n':
blank++;
break;
case '0':case '1':case'2':case'3':case'4': //注意判断数字时不能写成case ch>='0'&&ch<='9'
case '5':case '6':case'7':case'8':case'9':
digit++;
break;
default :
other++;
break;
}
}
printf("blank=%d,digit=%d,other=%d",blank,digit,other);
return 0;
}

这段代码展示了如何利用C语言的switch语句统计输入的15个字符中空格、数字和其他字符的数量。程序会忽略最后一个回车,并分别输出三类字符的计数。

2586

被折叠的 条评论
为什么被折叠?



