美文网首页考研
[考研]东大C语言编程题——11统计字符

[考研]东大C语言编程题——11统计字符

作者: LINCHUAN114 | 来源:发表于2017-10-16 17:27 被阅读28次

title: '[考研]东大C语言编程题——11统计字符'
date: 2017-10-16 16:33:19
tags: [考研,数据结构]
thumbnail:https://img.haomeiwen.com/i3635391/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;
}

运行结果

运行结果

代码地址

相关文章

网友评论

    本文标题:[考研]东大C语言编程题——11统计字符

    本文链接:https://www.haomeiwen.com/subject/fizkuxtx.html