统计数字字符和空格:
题要求编写程序,输入一行字符,统计其中数字字符、空格和其他字符的个数。建议使用switch语句编写。
输入格式:
输入在一行中给出若干字符,最后一个回车表示输入结束,不算在内。
输出格式:
在一行内按照
blank = 空格个数, digit = 数字字符个数, other = 其他字符个数
的格式输出。请注意,等号的左右各有一个空格,逗号后有一个空格。
#include<stdio.h>
int main(){
char ch;
int blank,digit,other;
blank=digit=other=0;
ch=getchar();
while(ch!='\n')
{
switch(ch)
{
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':digit++;
break;
case ' ':blank++;
break;
default:other++;
break;
}
ch=getchar();
}
printf("blank = %d, digit = %d, other = %d",blank,digit,other);
return 0;
}
今日份学习心得!!!他来了——
今天主要学习了switch开关语句和循环语句中的while类型
本题在while循环语句中内嵌了switch开关语句
在理解题意的基础上进行编写,编写期间要注意一些细节问题,比如循环语句最后要有一句以便返回,而switch开关语句要记得最后break打断语句进行