列表

详情


CPP52. 统计字符串中各类型字符的个数

描述

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

输入描述

输入任意字符串(注:测试用例输入字符串保证无中文字符)

输出描述

输出字符串中包含的英文字母个数,数字个数,空格个数,其它字符个数

示例1

输入:

hello 123, nihao. 234

输出:

letter:10 digit:6 space:3 other:2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2022-07-23

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;

    char buf[1024] = {0};
    cin.getline(buf, sizeof(buf));

    // write your code here......
    for (int i = 0; buf[i] != '\0'; i++) {
        if (isalpha(buf[i]))
                letter++;
                else if (isdigit(buf[i]))
                             digit++;
                             else if (isspace(buf[i]))
                                          space++;
                                          else
                                              other++;

                }

                 cout << "letter:" << letter << " digit:" << digit << " space:" << space <<
                 " other:" << other << endl;

        return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2022-04-17

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;
    
    char buf[100] = {0};
    cin.getline(buf, sizeof(buf));

    // write your code here......
    char *p=buf;
    while(*p != '\0')
    {
        if((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
        {
            letter++;
        }
        else if(*p >= '0' && *p <= '9')
        {
            digit++;
        }
        else if(*p == ' ')
        {
            space++;
        }                                                                                                                                                                                                                
        else
        {
            other++;
        }
        p++;
    }
    

    cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;
    
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2022-01-26

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;
    
    char buf[1024] = {0};
    cin.getline(buf, sizeof(buf));

    // write your code here......
    int i=0;
    while(buf[i]!='\0')
    {
        if((buf[i]>='A'&&buf[i]<='Z')||(buf[i]>='a'&&buf[i]<='z')) letter++;
        else if(buf[i]>='1'&&buf[i]<='9') digit++;
        else if(buf[i]==' ') space++;
        else  other++;
        i++;
    }

    cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2021-10-23

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;
    
    char buf[1024] = {0};
    cin.getline(buf, sizeof(buf));
    for(int i=0;buf[i]!='\0';i++){
      char c =buf[i] ;
        if(c>='a'&&c<='z'||c>='A'&&c<='Z')
            letter++;
        else if(c>='0'&&c<='9')
             digit++;
        else if(c==' ')
            space++;
        else
            other++;
        
        
    }
    // write your code here......

    cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;

	return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-12-07

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	getline(cin,s);
	int a,b,c,d;
	a=b=c=d=0;
	for(int i=0;i<s.size();i++){
		if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z'){
			a++;
		}
		else if(s[i]==' '){
			b++;
		}
		else if(s[i]>='0'&&s[i]<='9'){
			c++;
		}
		else{
			d++;
		}
	}
	cout<<"letter:"<<a<<" digit:"<<c<<" space:"<<b<<" other:"<<d;
	return 0;
}

上一题