1695 - C语言程序设计教程(第三版)课后习题6.2
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
Input
一行字符
Output
统计值
Examples
Input
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
Output
23 16 2 4
Solution C
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char c; int q=0,e=0,r=0,w=0; while((c=getchar())!='\n') { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) q++; else if(c==' ') w++; else if(c>='0'&&c<='9') e++; else r++; } printf("%d %d %d %d",q,e,w,r); return 0; }
Solution C++
#include<iostream> #include<cstdio> using namespace std; int main() { char c; int letter=0,digit=0,space=0,other=0; while ((c=getchar()) != '\n') if (isalpha(c)) letter++; else if (isalnum(c)) digit++; else if (c==' ') space++; else other++; cout<<letter<<" "<<digit<<" "<<space<<" "<<other<<endl; return 0; }