1163 - C语言6.11
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
Input
一行字符。
Output
分别输出这行字符中的英文字母、空格、数字和其他字符的个数,用空格隔开。
请注意行尾输出换行。
Examples
Input
What are you doing? 123456
Output
15 4 6 1
Solution C
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { char c; int e; int b; int a; int d; e=0; b=0; a=0; d=0; while((c=getchar())!='\n') { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) e++; else if(c>='0'&&c<='9') b++; else if(c==' ') a++; else d++; } printf("%d %d %d %d\n",e,a,b,d); return 0; }
Solution C++
#include <stdio.h> #include <math.h> int main() { char c; int letter, space, digit, other; letter = 0; space = 0; digit = 0; other = 0; while ((c = getchar()) != '\n') { if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) letter++; else if (c == ' ') space++; else if ('0' <= c && c <= '9') digit++; else other++; } printf("%d %d %d %d\n", letter, space, digit, other); return 0; }