title: ‘[考研]东大c言语编程题——11计算字符’
date: 2021-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+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;
}
运转成果
运转成果
代码地址