用c++写一个函数,统计字符串中大写,小写和数字字母个数
时间: 2024-03-24 08:14:44 浏览: 78
c++/C语言进制转换
4星 · 用户满意度95%
```c
#include <stdio.h>
#include <ctype.h>
void count(char *str, int *upper, int *lower, int *digit, int *alpha) {
*upper = 0;
*lower = 0;
*digit = 0;
*alpha = 0;
while (*str) {
if (isupper(*str))
(*upper)++;
else if (islower(*str))
(*lower)++;
else if (isdigit(*str))
(*digit)++;
if (isalpha(*str))
(*alpha)++;
str++;
}
}
int main() {
char str[] = "Hello World 123";
int upper, lower, digit, alpha;
count(str, &upper, &lower, &digit, &alpha);
printf("Upper case: %d\n", upper);
printf("Lower case: %d\n", lower);
printf("Digit: %d\n", digit);
printf("Alphabet: %d\n", alpha);
return 0;
}
```
阅读全文