输入一行文字,用指针的方法统计其中大写字母,小写字母,空格,数字以及其他字符的个数利用c语言
时间: 2023-08-02 08:11:27 浏览: 189
下面是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h> // 包含了用于字符判断的函数,如 isdigit, isalpha 等
int main() {
char str[100];
int upper = 0, lower = 0, space = 0, digit = 0, other = 0;
char *p;
printf("请输入一行文字:\n");
fgets(str, 100, stdin); // 使用 fgets 函数读取一行文字
p = str; // 指向字符串的第一个字符
while (*p != '\0') { // 如果没有到字符串末尾
if (isupper(*p)) { // 判断是否为大写字母
upper++;
} else if (islower(*p)) { // 判断是否为小写字母
lower++;
} else if (isspace(*p)) { // 判断是否为空格
space++;
} else if (isdigit(*p)) { // 判断是否为数字
digit++;
} else { // 其他字符
other++;
}
p++; // 指向下一个字符
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("空格:%d\n", space);
printf("数字:%d\n", digit);
printf("其他字符:%d\n", other);
return 0;
}
```
代码中使用指针 p 指向字符串的第一个字符,然后遍历整个字符串,判断每个字符的类型,统计出现的次数。注意,这里使用了 ctype.h 库中的一些函数,可以方便地进行字符判断。
阅读全文
相关推荐
















