SummerVocation_Learning--java的String类运用

本文介绍了一种方法,用于统计给定字符串中的大写字母、小写字母及其他字符的数量。通过三种不同的实现思路,包括直接字符比较、利用预定义字符串进行匹配以及使用Java内置的Character类方法,展示了如何高效地完成这一任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 题目:

            编写一个程序,输出一个字符串中的大写字母数,小写字母数,及其它字母数。

    

    思路1:

            可以先遍历整个字符串,在判断每个字符的类型。

 1 public class TestString {
 2 
 3     public static void main(String[] args) {
 4         String s = "abcDFEGHadga@#%@454sfgha";
 5         int ucount=0,lcount=0,ocount=0;
 6         for (int i = 0; i < s.length(); i++) {
 7             if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
 8                 lcount++;
 9             }
10             else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
11                 ucount++;
12             }
13             else {
14                 ocount++;
15             }
16         }
17         System.out.println("大写字母数:"+ucount+"\n"+"小写字母数:"+lcount);
18         System.out.println("其它字符数:"+ocount);
19         /*
20          * 大写字母数:5
21          * 小写字母数:12
22          * 其他字母数:7
23          */
24     }
25 
26 }

  

  思路2:

           可以先定义好一个包含所有大写字母的字符串和一个包含所有小写字母的字符串,在进行判断。

 1 public class TestString {
 2 
 3     public static void main(String[] args) {
 4         String s = "abcDFEGHadga@#%@454sfgha";
 5         int ucount=0,lcount=0,ocount=0;
 6         String sL = "abcdefghijklmnopqrstuvwxyz";
 7         String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 8         char c;
 9         for (int i = 0; i < s.length(); i++) {
10             c = s.charAt(i);
11             if (sL.indexOf(c) != -1) {
12                 lcount++;
13             }
14             else if (sU.indexOf(c) != -1) {
15                 ucount++;
16             }
17             else {
18                 ocount++;
19             }
20         }
21         System.out.println("大写字母数:"+ucount+"\n"+"小写字母数:"+lcount);
22         System.out.println("其它字符数:"+ocount);
23         /*
24          * 大写字母数:5
25          * 小写字母数:12
26          * 其他字母数:7
27          */
28     }
29 
30 }

另外在判断一个字母是小写字母还是大写字母的时候,可以用Character类里面的isLowerCase(char ch)和is UpperCase(ch).

如: 程序1可改为:

 1 public class TestString {
 2 
 3     public static void main(String[] args) {
 4         String s = "abcDFEGHadga@#%@454sfgha";
 5         int ucount=0,lcount=0,ocount=0;
 6         char c;
 7         for (int i = 0; i < s.length(); i++) {
 8             c = s.charAt(i);
 9             if (Character.isLowerCase(c)) {
10                 lcount++;
11             }
12             else if (Character.isUpperCase(c)) {
13                 ucount++;
14             }
15             else {
16                 ocount++;
17             }
18         }
19         System.out.println("大写字母数:"+ucount+"\n"+"小写字母数:"+lcount);
20         System.out.println("其它字符数:"+ocount);
21         /*
22          * 大写字母数:5
23          * 小写字母数:12
24          * 其他字母数:7
25          */
26     }
27 
28 }

 

转载于:https://www.cnblogs.com/DSYR/p/9292667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值