接收字符串,统计其中,大写字母,小写字母,空格,其他字符,数字,的函数方法

#include <stdio.h>
#include <string.h>
int  letter = 0, digit = 0, space = 0, other = 0, big = 0;
int  i;
void panduan(char str[80])
{
    gets(str);
    long len = strlen(str);
    for (i = 0; i < len; i++)
    {
        if ('a' <= str[i] && str[i] <= 'z' )
        {
            letter ++;
        }
        else if ('A' <= str[i] && str[i] <= 'Z') big++;
        else if ('0' <= str[i] && str[i] <= '9')
        {
            digit ++;
        }
        else if (str[i] == ' ' )
        {
            space ++;
        }
        else
            other ++;
    }
}

void main()
{
  char  a[80], b[80], c[80];

    for (int d = 0; d < 3; d++)
    {
        printf("\n请输入第%d行:\n", d);
       switch(d)
       {
           case 0: panduan(a); break;
           case 1: panduan(b); break;
           case 2: panduan(c); break;
       }
        
      


    }
    
     for (int d = 0; d < 3; d++)
    {
        
       switch(d)
       {
           case 0: printf("%s\n", a); break;
           case 1: printf("%s\n", b); break;
           case 2: printf("%s\n", c); break;
       }
        
      


    }
    
    printf("大写字母数:%d\n", big);
    printf( "小写字母数:%d\n", letter);
    printf("数字个数  :%d\n", digit);
    printf("空格个数  :%d\n", space);
    printf("其它字符  :%d\n", other);

运用的知识

  • 函数统计字符串的内容
  • 全局变量
  • 数组做函数参数
发布了18 篇原创文章 · 获赞 0 · 访问量 194

猜你喜欢

转载自blog.csdn.net/weixin_46456339/article/details/105337046