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

大哥大姐,请教下,输入一个字符串,统计其中的大写字母,小写字母,数字字符,空白。
最新回答
听说网名太长会被狗咬

2024-05-16 05:01:16

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",执行程序,输出测试结果,

退场

2024-05-16 04:10:29

#include

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;
}
//基本就这样,有不明白的在追问吧。
倾城花音

2024-05-16 00:16:24

变量upper、lower、space、digit、other分别表示大写字母、小写字母、空格、数字和其它类型的字符的数量,p初始指向字符数组的第一个单元,每次循环判断一个字符是属于大写字母,还是小写字母、空格、数字或者其它,对于的计数器加1
挺简单的