热门文章推荐
以下50道算法编程题访问量较大,包含常用语法,数据结构,解题思路等等,作为C语言学习者必练题目推荐给各位。创作不易,记得一键三连哦!!!
练习3-4 统计字符
本题要求编写程序,输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
输入格式:
输入为10个字符。最后一个回车表示输入结束,不算在内。
输出格式:
在一行内按照
letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数
的格式输出。
输入样例:
aZ &
09 Az
输出样例:
letter = 4, blank = 3, digit = 2, other = 1
代码:
#include
int main()
{
char str[10];
int i;
int letter=0;
int blank=0;
int digit=0;
int other=0;
for(i=0;i<10;i++)
{
scanf("%c",&str[i]);
if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z') letter++;
else if(str[i]==' '||str[i]=='\n') blank++;
else if(str[i]>='0'&&str[i]<='9') digit++;
else other++;
}
printf("letter = %d, blank = %d, digit = %d, other = %d\n",letter,blank,digit,other);
return 0;
}