1163 - C语言6.11

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

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

题目输入

一行字符。

题目输出

分别输出这行字符中的英文字母、空格、数字和其他字符的个数,用空格隔开。

请注意行尾输出换行。

输入/输出样例

输入格式

What are you doing? 123456

输出格式

15 4 6 1

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;
}


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;
}

Java解答

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        int a=0,b=0,c=0,d=0;
        String s = cin.nextLine();
        char[] ch= s.toCharArray();
        for(int i=0;i<ch.length;i++){
            if((ch[i]>='A'&&ch[i]<='Z')||(ch[i]>='a'&&ch[i]<='z'))
                a++;
            else if(ch[i]==' ')
                b++;
            else if(ch[i]>='0'&&ch[i]<='9')
                c++;
            else
                d++;
        }
        System.out.printf("%d %d %d %d\n", a,b,c,d);
    }
}

Python解答

c1 = c2 = c3 = c4 = 0
for i in raw_input():
  if i.isalpha():
    c1 = c1 + 1
  elif i.isspace():
    c2 = c2 + 1
  elif i.isdigit():
    c3 = c3 + 1
  else:
    c4 = c4 + 1
print c1,c2,c3,c4