通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 分数 10 作者 仇丽青 单位 山东科技大学
时间: 2023-09-26 08:09:25 浏览: 48
好的,这是一个关于字符统计的问题。以下是一个简单的 C++ 代码示例,可以实现该功能:
```c++
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char c;
int letter = 0, space = 0, digit = 0, other = 0;
cout << "请输入一行字符:" << endl;
while (cin.get(c)) {
if (isalpha(c)) letter++;
else if (isspace(c)) space++;
else if (isdigit(c)) digit++;
else other++;
}
cout << "字母个数:" << letter << endl;
cout << "空格个数:" << space << endl;
cout << "数字个数:" << digit << endl;
cout << "其他字符个数:" << other << endl;
return 0;
}
```
该程序通过 `cin.get(c)` 逐个读取输入的字符。然后使用 `isalpha()` 判断是否为字母,`isspace()` 判断是否为空格,`isdigit()` 判断是否为数字,如果不是以上三种字符,则视为其他字符。最后输出各种字符的个数。
希望这个程序可以帮到你!