C language | Statistical characters Chinese and English spaces, numbers and other

Example 52: Input a line of characters, and C language programming separately counts the number of English letters, spaces, numbers and other characters.

Analysis: First, you must manually enter the information, but the scanf function does not record spaces, so first use the getchar function for keyboard entry.

Source code demo:

#include<stdio.h>//头文件 
int main()//主函数 
{
    
    
  char input_Character;//定义字符变量 
  int letters=0,space=0,digit=0,other=0;//定义整型变量且赋初值 
  printf("请输入一行字符:");//提示语句 
  while((input_Character=getchar())!='\n') //getchar函数录入,可以把空格也算做字符 
  {
    
    
    if(input_Character>'a'&&input_Character<'z'||input_Character>'A'&&input_Character<'Z')//如果是字母 
    {
    
    
      letters++;//letters加1 
    }
    else if(input_Character==' ')//如果是空格 
    {
    
    
      space++;//sapce加1 
    }
    else if(input_Character>='0'&&input_Character<='9')//如果是数字 
    {
    
    
      digit++;//digit加1 
    }
    else
    {
    
    
      other++;//other加1 
    }
  }
  printf("字母:%d个\n",letters);//输出字母个数 
  printf("空格:%d个\n",space);//输出空格个数 
  printf("数字:%d个\n",digit);//输出数字个数 
  printf("其他字符:%d个\n",other);//输出其他字符个数 
  return 0;//主函数返回 
}

The compilation and running results are as follows:

请输入一行字符:kk 34!
字母:2
格:1
数字:2
其他字符:1

--------------------------------
Process exited after 15.16 seconds with return value 0
请按任意键继续. . .

Xiaolin guesses blindly, some readers do not understand this line of code:

while((input_Character=getchar())!='\n')

Readers who have played ACM should understand that Kobayashi is here to explain. The meaning of this code translation into Chinese is that when the input characters are many line breaks, that is, the keyboard input characters have not been clicked on the enter key to line break.

C language statistical characters Chinese and English space numbers and other
More cases can go to the official account: C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112204178