C语言刷题随记 —— 统计各种字符的个数_git

\

文章目录

  • 题目
  • 思路
  • 题解
  • 样例输出

题目

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

C语言刷题随记 —— 统计各种字符的个数_后端_02

思路

分别定义四个变量置零,利用 while 语句分别 英文字母、空格、数字 和 其它字符的个数。

题解

#include <stdio.h>

int main()
{
    char c;
    int letters=0,space=0,digit=0,others=0;
    
    printf("请输入一串字符:\n");
    
    while((c=getchar())!='\n')
    {
        if(c>='a'&&c<='z'||c>='A'&&c<='Z')	// 统计字符
            letters++;
            
        else if(c==' ')	// 统计空格
            space++;
            
        else if(c>='0'&&c<='9')	// 统计数字
            digit++;
            
        else	// 统计其它
            others++;
    }

    printf("统计:字符=%d 空格=%d 数字=%d 其它=%d\n",letters,
space,digit,others);
}

样例输出

C语言刷题随记 —— 统计各种字符的个数_后端_03

C语言刷题随记 —— 统计各种字符的个数_#include_04