从键盘输入一串字符串,已回车结束,分别统计输出其中数字、字母和其他字符的个数。

如题所述

#include <stdio.h>int main(){ char c; int letters=0,space=0,digit=0,others=0; printf("please input some characters\n"); while((c=getchar())!='\n') //循环输入字符,直到输入回车 { if(c>='a' && c<='z' || c>='A' && c<='Z') letters++; else if(c==' ') space++; else if(c>='0' && c<='9') digit++; else others++; } printf("统计:字母=%d 空格=%d 数字=%d 其它=%d\n",letters,space,digit,others); return 0;}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-05-19
#include#includeint main(){int i,a[5]; char s[200]; gets(s); for(i=0;s[i];i++) if(isupper(s[i]))a[0]++; else if(islower(s[i]))a[1]++;\t else if(s[i]==' ')a[2]++;\t else if(isdigit(s[i]))a[3]++;\t\t else a[4]++; printf("英文大写字母有%d个",a[0]); printf("英文小写字母有%d个",a[1]); printf("空格有%d个",a[2]); printf("数字有%d个",a[3]); printf("其它字符有%d个",a[4]); return 0;}