题目内容: 输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。
例: (1)输入:I love hebeu! 输出:character:10,space:2,digit:0,others:1 (2)输入:2020, have a brilliant year! 输出:character:18,space:4,digit:4,others:2
答案:
1#include<stdio.h> 2int main() 3{ 4 char c; 5 int letters=0,spaces=0,digits=0,others=0; 6 while((c=getchar())!='\n') 7 { 8 if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) 9 letters++; 10 else if(c>='0'&&c<='9') 11 digits++; 12 else if(c==' ') 13 spaces++; 14 else 15 others++; 16 } 17 printf("character:%d,space:%d,digit:%d,others:%d",letters,spaces,digits,others); 18 return 0; 19}
