C language | Counting characters in articles

Example 65: There is an article with 3 lines of text, and each line has 80 characters. C language programming realizes the statistics of the number of English uppercase letters, lowercase letters, numbers, spaces and other characters.

Problem-solving idea: the row number of the array text is 0 2, but when the user is prompted to enter each row of data, Xiaolin here asks the reader to enter the first row, second row, and third row instead of row 0, row 1, and row 3. Line 2, this is completely to take care of the reader's habit. For this reason, use i+1 instead of i when outputting the number of lines in the sixth line of the program. This does not affect the processing of the array by the program, and the first subscript value of the array elsewhere in the program is still 0 2.

Source code demo:

#include<stdio.h>//头文件 
int main()//主函数 
{
    
    
  int i,j,lower,number,space,other,capital;//定义整型变量 
  char text[3][80];//定义字符数组 
  capital=0;//赋初值 
  lower=0;//赋初值 
  number=0;//赋初值 
  space=0;//赋初值 
  other=0;//赋初值 
  for(i=0;i<3;i++)
  {
    
     //设置3行 
    printf("请随意输入一行:\n"); //注意录入的必须是英文状态下的符号 
    gets(text[i]); //gets函数可以录入空格 
    for(j=0;j<80&&text[i][j]!='\0';j++)
    {
    
     
      if(text[i][j]>='A'&&text[i][j]<='Z')//如果是大写 
      {
    
    
        capital++;
      }
      else if(text[i][j]>='a'&&text[i][j]<='z')//如果是小写 
      {
    
    
        lower++;
      }
      else if(text[i][j]>='0'&&text[i][j]<='9')//如果是数字 
      {
    
    
        number++;
      }
      else if(text[i][j]==' ')//如果是空格 
      {
    
    
        space++;
      }
      else   //其他 
      {
    
    
        other++;
      }
    }
  }
  printf("\n输出结果:\n");//提示语句 
  printf("大写字母 :%d\n",capital);
  printf("小写字母 :%d\n",lower); 
  printf("数字 :%d\n",number);
  printf("空格 :%d\n",space);
  printf("其他字符 :%d\n",other);
  return 0;//主函数返回值为0 
}

The compilation and running results are as follows:

请随意输入一行:
I love C yuyan
请随意输入一行:
123
请随意输入一行:
haha

输出结果:
大写字母 :2
小写字母 :13
数字 :3
空格 :3
其他字符 :0

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

Above, if you see it and think it is helpful to you, please give Xiaolin a thumbs up and share it with the people around him, so that Xiaolin will also have the motivation to update, thank you fathers and villagers~

Character description in C language statistical articles

More cases can go to the public account: C language entry to mastery

Guess you like

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