以void fun设计C++:键盘输入一个字符串,统计其中的大写字母、小写字母、数字字母、其他字符的个数。
时间: 2023-12-13 10:04:20 浏览: 25
下面是一个以 `void fun` 设计的 C++ 函数,用于统计字符串中的大写字母、小写字母、数字和其他字符的个数:
```c++
#include <iostream>
#include <cctype>
using namespace std;
void fun(string s){
int upper = 0, lower = 0, digit = 0, other = 0;
for(char ch : s){
if(isupper(ch)){
upper++;
}
else if(islower(ch)){
lower++;
}
else if(isdigit(ch)){
digit++;
}
else{
other++;
}
}
cout << "大写字母个数:" << upper << endl;
cout << "小写字母个数:" << lower << endl;
cout << "数字个数:" << digit << endl;
cout << "其他字符个数:" << other << endl;
}
int main(){
string s;
cout << "请输入字符串:";
getline(cin, s); // 使用 getline 函数读取一行字符串
fun(s);
return 0;
}
```
在上面的代码中,我们使用了 `isupper`、`islower` 和 `isdigit` 函数来判断字符是否为大写字母、小写字母和数字,使用了一个计数器来统计各种类型字符出现的次数,并最终输出结果。