输入一个字符串,统计其中的大写字母,小写字母,数字字符。

帮忙解读一下程序,看不懂哦。

#include <stdio.h>
int main()
{
char str[256];
char *p;
int upper = 0;
int lower = 0;
int space = 0;
int digit = 0;
int other = 0;
p = str;
gets(p);

while(*p)
{
if(*p>='A' && *p<='Z')
{
upper++;
}
else if(*p>='a' && *p<='z')
{
lower++;
}
else if(*p == ' ')
{
space++;
}
else if(*p>='0' && *p<='9')
{
digit++;
}
else
{
other++;
}
p++;
}
printf("upper = %d\n",upper);
printf("lower = %d\n",lower);
printf("space = %d\n",space);
printf("digit = %d\n",digit);
printf("other = %d\n",other);
return 0;
}

1、编写代码,统计大写字母数量,

int cnt_upper = 0;

String regex_u = "[A-Z]";

Pattern p3 = Pattern.compile(regex_u);

java.util.regex.Matcher m3 = p3.matcher(str);

while (m3.find()) {

cnt_upper++;

}

System.out.print("大写字母数量:");

System.out.println(cnt_upper);

2、编写代码,统计小写字母数量,

int cnt_lower = 0;

String regex_l = "[a-z]";

p3 = Pattern.compile(regex_l);

m3 = p3.matcher(str);

while (m3.find()) {

cnt_lower++;

}

System.out.print("小写字母数量:");

System.out.println(cnt_lower);

3、编写代码,统计数字数量,

int cnt_num = 0;

String regex_n = "[0-9]";

p3 = Pattern.compile(regex_n);

m3 = p3.matcher(str);

while (m3.find()) {

cnt_num++;

}


System.out.print("数字数量:");

System.out.println(cnt_num);

4、输入测试字符串,String str = "A123Bde456EfG",执行程序,输出测试结果,

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-12-16
#include <stdio.h>
int main()
{
char str[256];
char *p;
int upper = 0;
int lower = 0;
int space = 0;
int digit = 0;
int other = 0;
p = str; // P指针指向数组第一个元素 str[0]
gets(p);

while(*p) // P不为空的时候继续下面的
{
if(*p>='A' && *p<='Z') // 判断是否为大写
{
upper++; // 统计大写字母个数
}
else if(*p>='a' && *p<='z') //是否为小写
{
lower++; //统计小写个数
}
else if(*p == ' ') // 判断是否为“ ”
{
space++; //统计个数
}
else if(*p>='0' && *p<='9') // 判断是否为数字
{
digit++; // 统计数字个数
}
else
{
other++; //剩下的是其他字符的 统计个数
}
p++; //指针后移
}
printf("upper = %d\n",upper); // 输出
printf("lower = %d\n",lower); // 输出
printf("space = %d\n",space);// 输出
printf("digit = %d\n",digit);// 输出
printf("other = %d\n",other);// 输出
return 0;
}
//基本就这样,有不明白的在追问吧。本回答被提问者采纳
相似回答