Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
118138 冯诚阳 统计字符串 C++ Accepted 1 MS 272 KB 926 2025-04-24 18:57:42

Tests(1/1):


Code:

#include <iostream> #include <cctype> // 用于字符分类函数 using namespace std; void countCharacters(const string &str, int &letters, int &digits, int &spaces, int &others) { letters = digits = spaces = others = 0; for (char c : str) { if (isalpha(c)) { // 判断是否为字母 letters++; } else if (isdigit(c)) { // 判断是否为数字 digits++; } else if (isspace(c)) { // 判断是否为空白字符(包括空格、制表符等) spaces++; } else { others++; } } } int main() { string input; getline(cin, input); // 读取可能包含空格的整行输入 int letters, digits, spaces, others; countCharacters(input, letters, digits, spaces, others); cout << letters << " " << digits << " " << spaces << " " << others << endl; return 0; }