Hardy.z 2023-12-02 21:29 采纳率: 70%
浏览 2
已结题

c++问题中统计字符个数

img


统计字符个数。从in.txt顺序读取所有的字符,要求统计出所有大写字母个数,阿拉伯数字个数及空格字符个数

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-03 10:01
    关注

    【以下回答由 GPT 生成】

    ```cpp
    #include <iostream>
    #include <fstream>
    #include <cctype>
    
    int main() {
        std::ifstream in("in.txt");
        std::ofstream out("out.txt");
    
        if (!in.is_open()) {
            std::cerr << "Unable to open input file." << std::endl;
            return 1;
        }
    
        if (!out.is_open()) {
            std::cerr << "Unable to open output file." << std::endl;
            return 1;
        }
    
        int upper_count = 0;
        int lower_count = 0;
        int digit_count = 0;
        int space_count = 0;
    
        char c;
        while (in.get(c)) {
            if (isupper(c)) {
                upper_count++;
            } else if (islower(c)) {
                lower_count++;
            } else if (isdigit(c)) {
                digit_count++;
            } else if (isspace(c)) {
                space_count++;
            }
        }
    
        out << upper_count << std::endl;
        out << lower_count << std::endl;
        out << digit_count << std::endl;
        out << space_count << std::endl;
    
        in.close();
        out.close();
    
        return 0;
    }
    


    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月12日
  • 已采纳回答 12月4日
  • 创建了问题 12月2日