top1编程
← 返回题目
题解

【入门】标题统计

1 条题解

  • 0
    @ 2026-7-29 0:15:12
    #include <iostream>
    #include <string>  // 显式包含string头文件,避免编译错误
    using namespace std;
    
    int main() {
        // 1. 读取整行输入(包含空格)
        string s;
        getline(cin, s);  // 替换cin>>s,能读取带空格的整行
        
        // 2. 统计有效字符(排除空格和换行符)
        int count = 0;
        for (char c : s) {  // 遍历字符串中的每个字符
            // 只统计非空格、非换行的字符
            if (c != ' ' && c != '&#92;n') {
                count++;
            }
        }
        
        // 3. 输出结果
        cout << count << endl;
        return 0;
    }
    
    • 1