编程统计字符个数,输出大写,小写,数字,空格
时间: 2024-01-13 14:05:36 浏览: 54
以下是C++统计字符个数并输出大写、小写、数字、空格的示例代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100];
int len, i, upper = 0, lower = 0, digit = 0, space = 0, other = 0;
cout << "请输入一个字符串:";
cin.getline(str, 100);
len = strlen(str);
for (i = 0; i < len; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
upper++;
else if (str[i] >= 'a' && str[i] <= 'z')
lower++;
else if (str[i] >= '0' && str[i] <= '9')
digit++;
else if (str[i] == ' ')
space++;
else
other++;
}
cout << "大写字母个数:" << upper << endl;
cout << "小写字母个数:" << lower << endl;
cout << "数字个数:" << digit << endl;
cout << "空格个数:" << space << endl;
return 0;
}
```
阅读全文