首页 > 编程语言 >编写一个C程序,使得读取输入的字符串,统计并输出字符串中大写字母、小写字母、数字和其他字符的数量。

编写一个C程序,使得读取输入的字符串,统计并输出字符串中大写字母、小写字母、数字和其他字符的数量。

时间:2024-06-21 12:28:45浏览次数:10  
标签:字符 小写字母 大写字母 ++ str printf 字符串

#include <stdio.h>
#include <string.h>


int main()
{
    char str[100];        //赋予字符长度为100的存储空间
    int i,uppercase=0,lowpercase=0,number=0,other=0;        //变量初始化
    printf("请输入字符串:");
    gets(str);        //输入字符串,str是已定义的字符数组
    
    for(i=0;str[i]!='\0';i++)        //i的初始值为零,字符数组第i个不等于“\0”,即空字符,则i++
    {
        if(str[i]>='A'&&str[i]<='Z')        // A <= i <= Z范围
            uppercase++;
        else if(str[i]>='a'&&str[i]<='z')        // a <= i <= z范围
            lowpercase++;
        else if(str[i]>='0'&&str[i]<='9')        // 0 <= i <= 9范围
            number++;
        else        // 其他字符串
            other++;
    }
    printf("大写字母数:%d\n",uppercase);        //打印大写字母字符数
    printf("小写字母数:%d\n",lowpercase);        //打印小写字母字符数
    printf("数字数:%d\n",number);        //打印数字字符数
    printf("其他字符数:%d\n",other);        //打印其他字符总数
    return 0;
 } 

标签:字符,小写字母,大写字母,++,str,printf,字符串
From: https://blog.csdn.net/2301_80965364/article/details/139752364

相关文章