1695 - C语言程序设计教程(第三版)课后习题6.2
时间限制 : 1 秒
内存限制 : 32 MB
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
题目输入
一行字符
题目输出
统计值
输入/输出样例
输入格式
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
输出格式
23 16 2 4
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; }
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; }