
统计字符个数。从in.txt顺序读取所有的字符,要求统计出所有大写字母个数,阿拉伯数字个数及空格字符个数
关注【以下回答由 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;
}
【相关推荐】