C语言 判断字符串中大写小写个数和数字个数

#include<stdio.h>
#define num 1000

void countUpperLetter(char []);
void countLowerLetter(char []);
void countDigit(char []);

int main()
{
char array[num];
printf("请输入一段文章:\n");
gets(array);

countUpperLetter(array);
countLowerLetter(array);
countDigit(array);

return 0;
}

void countUpperLetter(char s1[])
{
int a=0;
for(int i=0;i<num;i++)
{
if(('A'<=s1[i])&&(s1[i]<='Z')) a++;
}
printf("大写字母:%d\n",a);
}

void countLowerLetter(char s1[])
{
int a=0;
for(int i=0;i<num;i++)
{
if(('a'<=s1[i])&&(s1[i]<='z')) a++;
}
printf("小写字母:%d\n",a);

}

void countDigit(char s1[])
{
int a=0;
for(int i=0;i<num;i++)
{
if(('0'<=s1[i])&&(s1[i]<='9')) a++;
}
printf("数字:%d\n",a);
}
都是错误的。

你改成这样

#include<stdio.h>
#define num 1000

void countUpperLetter(char []);
void countLowerLetter(char []);
void countDigit(char []);

int main()
{
char array[num];
printf("请输入一段文章:\n");
gets(array);

countUpperLetter(array);
countLowerLetter(array);
countDigit(array);
return 0;
}

void countUpperLetter(char s1[])
{
int a=0;
for(int i=0;i<num && s1[i]!='\0';i++)
{
if(('A'<=s1[i])&&(s1[i]<='Z')) a++;
}
printf("大写字母:%d\n",a);
}

void countLowerLetter(char s1[])
{
int a=0;
for(int i=0;i<num && s1[i]!='\0';i++)
{
if(('a'<=s1[i])&&(s1[i]<='z')) a++;
}
printf("小写字母:%d\n",a);

}

void countDigit(char s1[])
{
int a=0;
for(int i=0;i<num && s1[i]!='\0';i++)
{
if(('0'<=s1[i])&&(s1[i]<='9')) a++;
}
printf("数字:%d\n",a);
}


我测试了一下没有错误。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-06-30

统计字符串中大写小写个数和数字个数,过程:

    定义三个变量u,l,d,并初始化为0

    遍历字符串,循环检查每一个字符,

          如果是大写字母,则累加u

          如果是小写字母,则累加l

          如果是数字,则累加d

          如果是字符串结束符,则结束循环

    循环结束,输出统计结果。

参考代码:

#include<stdio.h>
#define num 1000
int main()
{
    int u=0,l=0,d=0;
    int i;
char array[num];
printf("请输入一段文章:\n");
gets(array);

for( i=0;array[i]!='\0'; i++ ) //输入的字符不一定是num个,所以,只需要检查到字符串结束符就可以了
{
if('A'<=array[i] && array[i]<='Z') u++;
else if('a'<=array[i] && array[i]<='z') l++;
else if('0'<=array[i] && array[i]<='9') d++;
}
printf("大写:%d\n小写:%d\n数字:%d\n", u,l,d );
return 0;
}

运行:

请输入一段文章:

helloworld2015-ChinaUnix博客

大写:2

小写:17

数字:4

Press any key to continue

第2个回答  推荐于2018-03-02
#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch[512]  = "This Is A String234777";
    int i = 0;
    int num = 0;
    int b = 0;
    int a = 0;
    for(i=0;ch[i]!='\0';i++)
    {
        if(isdigit(ch[i]))
            num++;
        if(islower(ch[i]))
            a++;
        if(isupper(ch[i]))
            b++;
    }

    printf("the number of digit:%d\n",num);
    printf("the number of lower:%d\n",a);
    printf("the number of upper:%d\n",b);
}

本回答被网友采纳
第3个回答  2014-12-12
因为array没初始化,里边有脏数据。在主函数里把 array 初始化一下:

int main()
{
char array[num];

memset(array,0,sizeof(array));

printf("请输入一段文章:\n");
gets(array);

countUpperLetter(array);
countLowerLetter(array);
countDigit(array);

return 0;
}
第4个回答  2014-12-12
gets(array); 是从array[0]开始扫描直到srray[999]中遇到'\0' 可能的值是你输入的个数 到 999
所以并不是你输入字符的个数

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网