请用指针编程实现:输入一行字符串,输出其中大写字母、小写字母、空格、数字以及其他字符的个数。用C语言编写
时间: 2024-05-05 09:22:42 浏览: 147
以下是用指针编程实现的C语言代码:
#include <stdio.h>
#include <ctype.h>
void count_chars(char *str) {
int upper = 0, lower = 0, space = 0, digit = 0, other = 0;
while (*str) {
if (isupper(*str)) {
upper++;
} else if (islower(*str)) {
lower++;
} else if (*str == ' ') {
space++;
} else if (isdigit(*str)) {
digit++;
} else {
other++;
}
str++;
}
printf("Upper case letters: %d\n", upper);
printf("Lower case letters: %d\n", lower);
printf("Spaces: %d\n", space);
printf("Digits: %d\n", digit);
printf("Other characters: %d\n", other);
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
count_chars(str);
return 0;
}
这个程序中,count_chars
函数遍历输入的字符串并计算其中大写字母、小写字母、空格、数字以及其他字符的个数。main
函数中获取用户输入的字符串,然后调用count_chars
函数并打印结果。
相关推荐
















