第一种方法,代码如下:
#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
请按任意键继续. . .