#include<stdio.h>
int main()
{
char *p,s[20];
int space=0,upper=0,lower=0,other=0,digital=0,i=0;
printf("please input string:");
while((s[i]=getchar())!='/n')//(s[i]=getchar())一定要加括号,否则s[i]变量没有真正赋值!
i++;
p=&s[0];
while(*p!='/n')
{
if(('A'<=*p)&&('Z'>=*p))//字符串常量需要放在左边变量放在其右边,否则表达编译出错!
++upper;//++upper是先加1,如果upper++则是后加1,在这里一定要用++upper;否则少计数一个!
else if(('a'<=*p)&&('z'>=*p))
++lower;
else if(('0'<=*p)&&('9'>=*p))
++digital;
else if(*p==' ')
++space;
else
++other;
p++;
}
printf("the digital number is %4d/n",digital);
printf("the upper number is %4d/n",upper);
printf("the lower number is %4d/n",lower);
printf("the space number is %4d/n",space);
return 0;
}
对输入字符串计数!
最新推荐文章于 2022-07-15 15:53:07 发布