输入一行文字,找出其中的大写字母、小写字母、空格、数字以及其他的字符各有多少

第一种方法,代码如下:

#include <stdio.h>
main()
{
char str[100];
int i,d=0,D=0,k=0,n=0,q=0;
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='0'&&str[i]<='9')
	n++;
else if(str[i]>='a'&&str[i]<='z')
	d++;
else if(str[i]>='A'&&str[i]<='Z')
	D++;
else if(str[i]==' ')       
	k++;
else q++;
}
printf("数字:n=%d  小写字母:d=%d  大写字母:D=%d  空格:k=%d  其他:q=%d\n",n,d,D,k,q);
}

第二种方法,代码如下:

#include <stdio.h>
main()
{
    char str[100];
    int i=0,d=0,D=0,k=0,n=0,q=0;
    gets(str);
    while(str[i]!='\0')
    {
        if(str[i]>=48&&str[i]<=57)
	        n++;
        else if(str[i]>=97&&str[i]<=122)
	        d++;
        else if(str[i]>=65&&str[i]<=90)
	        D++;
        else if(str[i]==32)     //空格的ASCLL码值为32      
        	k++;
        else q++;
        i++;
}
printf("数字:n=%d  小写字母:d=%d  大写字母:D=%d  空格:k=%d  其他:q=%d\n",n,d,D,k,q);
}

结果如下:

My teacher is Zhang,she is 35 age.
数字:n=2  小写字母:d=22  大写字母:D=2  空格:k=6  其他:q=2
请按任意键继续. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值