输入一行字符,先转换为大写,并用string的isdigit()、 isspace()、 isalpha()分别统计出其中英文字母、空格、数字和其它字符的个数。
时间: 2024-06-12 13:11:34 浏览: 5
可以使用以下代码实现:
```c++
#include <iostream>
#include <string>
#include <cctype> // 包含isdigit()、isspace()、isalpha()函数的头文件
using namespace std;
int main() {
string str;
int alpha_count = 0, space_count = 0, digit_count = 0, other_count = 0;
cout << "请输入一行字符:";
getline(cin, str); // 使用getline()函数读取一行字符
// 将字符串中的字母转换为大写
for (int i = 0; i < str.length(); i++) {
if (isalpha(str[i])) {
str[i] = toupper(str[i]);
}
}
// 统计各种字符的个数
for (int i = 0; i < str.length(); i++) {
if (isalpha(str[i])) {
alpha_count++;
} else if (isspace(str[i])) {
space_count++;
} else if (isdigit(str[i])) {
digit_count++;
} else {
other_count++;
}
}
// 输出结果
cout << "大写后的字符串为:" << str << endl;
cout << "其中英文字母个数为:" << alpha_count << endl;
cout << "其中空格个数为:" << space_count << endl;
cout << "其中数字个数为:" << digit_count << endl;
cout << "其中其它字符个数为:" << other_count << endl;
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)