c++,要求能够分别统计出一段字符abcABC 123?中的英文大写字母,小写字母,,数字,空格,以及其他字符的个数
时间: 2024-01-03 22:03:06 浏览: 28
好的,您的要求我已经了解了。下面是一个可以实现您要求的C++程序:
```c++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcABC 123?";
int count_upper = 0, count_lower = 0, count_digit = 0, count_space = 0, count_other = 0;
for(int i = 0; i < str.length(); i++)
{
char ch = str[i];
if(ch >= 'a' && ch <= 'z')
count_lower++;
else if(ch >= 'A' && ch <= 'Z')
count_upper++;
else if(ch >= '0' && ch <= '9')
count_digit++;
else if(ch == ' ')
count_space++;
else
count_other++;
}
cout << "count of upper case letters: " << count_upper << endl;
cout << "count of lower case letters: " << count_lower << endl;
cout << "count of digits: " << count_digit << endl;
cout << "count of spaces: " << count_space << endl;
cout << "count of other characters: " << count_other << endl;
return 0;
}
```
这个程序中,我们使用了一个循环遍历字符串str,然后根据字符的类型进行计数。如果字符是小写字母,count_lower计数器加1;如果字符是大写字母,count_upper计数器加1;如果字符是数字,count_digit计数器加1;如果字符是空格,count_space计数器加1;否则认为是其他字符,count_other计数器加1。最后输出各个计数器的值即可。