• SummerVocation_Learning--java的String类运用


     题目:

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

        

        思路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+"
    "+"小写字母数:"+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+"
    "+"小写字母数:"+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+"
    "+"小写字母数:"+lcount);
    20         System.out.println("其它字符数:"+ocount);
    21         /*
    22          * 大写字母数:5
    23          * 小写字母数:12
    24          * 其他字母数:7
    25          */
    26     }
    27 
    28 }
  • 相关阅读:
    排行榜 和 zset
    SpringBoot使用@ServerEndpoint无法依赖注入问题解决(WebSocket
    idea 全局内容搜索和替换
    fiddler不能监听 localhost和 127.0.0.1的问题
    fiddle4 弱网测试
    nginx代理websocket连接上限
    spring boot Websocket
    SpringBoot----跨域配置
    注解@Slf4j的使用
    word 转 pfd
  • 原文地址:https://www.cnblogs.com/DSYR/p/9292667.html
Copyright © 2020-2023  润新知