[考研]东大C语言编程题——11统计字符-简书(考研)




title: ‘[考研]东大c语言编程题——11统计字符’
date: 2017-10-16 16:33:19
tags: [考研,数据结构]
thumbnail:http://upload-images.jianshu.io/upload_images/3635391-6ba8d3822c99643d.jpg
toc: true


题目标记:??
题目描述
一片文章共有3行文字,每行有80个字符。要求分别统计出其中的大写字母、小写字母、数字、空格,以及其他字符的个数。

解题思路

对不同字符分别计数,通过ascii区间大小来判断。注意:这里不需要知道字符的ascii值是多少,直接通过比较就可以得到区间了,例如’p’字符在a~z之间,a<p<z这样判断即可。

除特定字符需要统计,剩下的即为其他字符
判断字符结束依据为是否等于\0

代码
#include <stdio.h>
int main()
{
int i=0,j,capital=0,low=0,number=0,space=0,other=0;
char text[3][80],temp;
while(i<3)
{
printf("请输入第%d行文字:\n",i
[考研]东大C语言编程题——11统计字符-简书(考研)插图
+1);
gets(text[i]);
for(j=0; j<80&&text[i][j]!='\0'; j++)
{
temp=text[i][j];
if(temp>='a'&&temp<='z') capital++;
else if(temp>='a'&&temp<='z') low++;
else if(temp>='0'&&temp<='9') number++;
else if(temp==' ') space++;
else other++;
}
printf("第%d行:大写字母:%d个,小写字母%d个,数字%d个,空格%d个,其他字符%d个\n",i,capital,low,number,space,other);
i++;
}
return 0;
}

运行结果

运行结果

代码地址

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

|京ICP备18012533号-328