有一个字符串,共20个字符,要求用C++统计出其中英文大写字母、小写字母、数字、空格、其他字符的个数。
时间: 2023-12-13 13:04:19 浏览: 120
可以使用C语言中的字符操作函数来统计各类字符的个数,具体实现如下:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[20] = "Hello, World! 2021";
int i, upper = 0, lower = 0, digit = 0, space = 0, other = 0;
for (i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
upper++;
} else if (islower(str[i])) {
lower++;
} else if (isdigit(str[i])) {
digit++;
} else if (isspace(str[i])) {
space++;
} else {
other++;
}
}
printf("Uppercase letters: %d\n", upper);
printf("Lowercase letters: %d\n", lower);
printf("Digits: %d\n", digit);
printf("Spaces: %d\n", space);
printf("Other characters: %d\n", other);
return 0;
}
```
运行结果:
```
Uppercase letters: 2
Lowercase letters: 12
Digits: 4
Spaces: 2
Other characters: 0
```
阅读全文
相关推荐
















