1 条题解

  • 0
    @ 2025-4-7 21:28:47

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char s[102];
    	int i,a,b,c,d,l;
    	gets(s);
    	l=strlen(s);
    	a=b=c=d=0;
    	for(i=0;i<l;i++)
    	{
    		if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))
    			a++;
    		else if(s[i]>='0'&&s[i]<='9')
    			b++;
    		else if(s[i]==' ')
    			c++;
    		else d++;		
    	}
    	printf("%d %d %d %d\n",a,b,c,d);
    	return 0;
    }
    

    C++ :

    #include <stdio.h>
    int main() {
    	void calc(char str[], int *letter, int *digit, int *space, int *other);
    	char str[101];
    	int letter, digit, space, other;
    	gets(str);
    	calc(str, &letter, &digit, &space, &other);
    	printf("%d %d %d %d\n", letter, digit, space, other);
    	return 0;
    }
    void calc(char str[], int *letter, int *digit, int *space, int *other) {
    	int i;
    	char c;
    	*letter = 0; *space = 0; *digit = 0; *other = 0;
    	for (i = 0;str[i] != '\0';i++) {
    		c = str[i];
    		if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'))
    			(*letter)++;
    		else if ('0' <= c && c <= '9')
    			(*digit)++;
    		else if (c == ' ')
    			(*space)++;
    		else
    			(*other)++;
    	}
    }
    
    

    Pascal :

    var 
      s:string;
    
    procedure p(s:string);
    var
      i,ia,ib,ic,id:longint;
      c:char;
    begin
      ia:=0; ib:=0; ic:=0; id:=0;
      for i:=1 to length(s) do begin //字母、数字、空格、其他
        c:=upcase(s[i]);
        if( c in ['A'..'Z'] ) then inc(ia)
          else if( c=' ' ) then inc(ib)
            else if( c in ['0'..'9'] ) then inc(ic)
              else inc(id);
      end;
      writeln(ia,' ',ic,' ',ib,' ',id); //ic,ib
    end;
    
    begin
      readln(s);
      p(s);
    end.
    
    
    
    

    Java :

    
    
    import java.util.Scanner;
    
    public class Main {
       private static Scanner s = new Scanner(System.in) ;
       
       public static void main(String[] args) {
    	   String str = s.nextLine() ;
    	   
    	   char[]c = str.toCharArray() ;
    	   int a = 0 ;
    	   int b = 0 ;
    	   int d = 0 ;
    	   int e = 0 ;
    	   int f = 0 ;
    	   for (int i = 0; i < c.length; i++) {
    		 if('A'<=c[i]&&c[i]<='Z'){
    			 a++ ;
    		 }
    		 else if('a'<=c[i]&&c[i]<='z'){
    			 a++ ;
    		 }
            else if(c[i]==' '){
            			d++ ; 
            		 }
            else if('0'<=c[i]&&c[i]<='9'){
            	 e++ ;
            }else f++ ;
            	   }
    	   
    	   System.out.println(a+" "+e+" "+d+" "+f) ;
        }
    }
    
    
    • 1

    信息

    ID
    1330
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者