3094 - 2002年秋浙江省计算机等级考试二级C 编程题(1)

输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。

题目输入

一行字符

题目输出

统计值

输入/输出样例

题目输入

aklsjflj123 sadf918u324 asdf91u32oasdf/.';123

题目输出

23 16 2 4

C语言解答

#include<stdio.h>
#include<stdlib.h>
int main(void){
    char c;
    int num1=0,num2=0,num3=0,num4=0;
    c=getchar();
    while(c!='\n'){
        if(('a'<=c&&c<='z')||('A'<=c&&c<='Z')){
            num1++;}
        else if('0'<=c&&c<='9'){
            num2++;}
        else if(c==' '){
            num3++;}
        else{
            num4++;}
    c=getchar();
    }
    printf("%d %d %d %d\n",num1,num2,num3,num4);
    return 0;
}

C++解答

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
	string s;
	getline(cin,s);
	int len=s.size();
	int letter=0,space=0,digit=0,other=0;
	for (int i=0; i<len; i++)
		if (isalpha(s[i])) letter++;
		else if (s[i]==' ') space++;
		else if (isdigit(s[i])) digit++;
		else other++;
	cout<<letter<<" "<<digit<<" "<<space<<" "<<other<<endl;
	return 0;
}
时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题