1 条题解

  • 0
    @ 2024-12-24 9:59:28

    C :

    #include<stdio.h>
    #include<string.h>
    main()
    {
    	char ch[10000];
    	int i,num1,num2,num3,num4;
    	while(gets(ch)!=NULL)
    	{
    		for(i=0,num1=0,num2=0,num3=0,num4=0;i<strlen(ch);i++)
    		{
    			if(ch[i]>='A'&&ch[i]<='Z'||ch[i]>='a'&&ch[i]<='z')
    			num1++;//character
    			else if(ch[i]==' ')
    			num2++;//space
    			else if(ch[i]>='0'&&ch[i]<='9')
    			num3++;//number
    			else
    			num4++;//else
    		}
    		printf("%d %d %d %d\n",num1,num3,num2,num4);
    	}
    }
    

    C++ :

    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;
    int main()
    {
    	string s;
    	getline(cin,s);
    	int letter=0,space=0,digit=0,other=0;
    	int t=s.size();
    	for (int i=0; i<t; i++)
    		if (isalpha(s[i])) letter++;
    		else if (isdigit(s[i])) digit++;
    		else if (s[i]==' ') space++;
    		else other++;
    	cout<<letter<<" "<<digit<<" "<<space<<" "<<other<<endl;
    	return 0;
    }
    
    • 1

    信息

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