c语言编程统计一个字符串中大写字母、小写字母、数字和其他字符的个数,要求用指针实现。
时间: 2023-11-19 21:15:16 浏览: 142
可以使用指针来实现对字符串中大写字母、小写字母、数字和其他字符的统计,具体代码如下:
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int upper_count = 0, lower_count = 0, digit_count = 0, other_count = 0;
char *ptr = str;
printf("请输入一个字符串:");
gets(str);
while (*ptr != '\0')
{
if (isupper(*ptr))
{
upper_count++;
}
else if (islower(*ptr))
{
lower_count++;
}
else if (isdigit(*ptr))
{
digit_count++;
}
else
{
other_count++;
}
ptr++;
}
printf("大写字母个数:%d\n", upper_count);
printf("小写字母个数:%d\n", lower_count);
printf("数字个数:%d\n", digit_count);
printf("其他字符个数:%d\n", other_count);
return 0;
}
上述代码中,首先定义了一个字符串数组 str
和四个计数器变量 upper_count
、lower_count
、digit_count
、other_count
,分别用来统计大写字母、小写字母、数字和其他字符的个数。
然后通过指针 ptr
指向字符串数组 str
,使用 while
循环遍历字符串中的每个字符,对于每个字符使用 isupper
、islower
、isdigit
函数进行判断,如果是大写字母、小写字母、数字或其他字符,则将相应的计数器加一。
最后输出结果即可。
相关推荐



















