题目描述:编写程序,输出字符串中的大写字母、小写小母和其他的个数。如有一个字符串"Helle, This is A test textfile.123456, tannk you!!",则其大写字母个数:3,小写字母个数:29,其他字符个数:18.

  这里提供了四种算法,第一种是我们比较好理解的,也属于硬编码问题,其他三种方法要借助JAVA语言的jdk提供的api。

方法一:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js分析字符串内容</title>
    <!--实现一个函数,输出某字符串里有几个大写字母,小写字母,数字,其他符号。字符串由形参指定 -->
    <script>
        var str = prompt("请随意输入大写字母小写字母数字及符号等");
        function analyze(aa){
            var a = 0;
            var A = 0;
            var n = 0;
            var other = 0;
            for (var i=0;i<aa.length;i++){
                var c = aa.substr(i,1); //aa.charAt(i);
                if (c>='a' && c<='z'){
                    a++;
                }else if(c>='A' && c<='Z'){
                    A++;
                }else if(c>='0' && c<='9'){
                    n++;
                }else{
                    other++;
                }
            }
            document.write("小写字母为"+a,"大写字母为"+A,"数字为"+n,"其他字符为"+other);
        }
    </script>
</head>
<body onload="analyze(str)">

</body>
</html>

  

[java] view plain copy
 
  1. //方法一:在利用每个字符的Unicode码在a~z之间,调用jdk提  
  2. //供的String类的charAt取出字符串每一个字符,逐个进行比较来判定  
  3.   
  4. class FindLetter {  
  5.     public static void main(String[] args) {  
  6.         String str = "Helle, This is A test textfile.123456, tannk you!!";  
  7.         int upCount = 0;  
  8.         int lowCount = 0;  
  9.         int otherCount = 0;  
  10.           
  11.         for(int i = 0; i < str.length(); i++) {  
  12.             char c = str.charAt(i);  
  13.             if(c >= 'a' && c <= 'z') {  
  14.                 lowCount++;  
  15.             } else if(c >= 'A' && c <= 'Z') {  
  16.                 upCount++;  
  17.             } else {  
  18.                 otherCount++;     
  19.             }  
  20.         }  
  21.         System.out.println("大写之母个数:" + upCount);  
  22.         System.out.println("小写字母个数:" + lowCount);  
  23.         System.out.println("其他字符个数:" + otherCount);  
  24.     }     
  25. }  

方法二:

 

[java] view plain copy
 
  1. //方法二:用jdk的Character类的isUpperCase方法和isLowerCase方法  
  2.   
  3. class FindLetter1 {  
  4.     public static void main(String[] args) {  
  5.         String str = "Helle, This is A test textfile.123456, tannk you!!";  
  6.         int upCount = 0;  
  7.         int lowCount = 0;  
  8.         int otherCount = 0;  
  9.           
  10.         for(int i = 0; i < str.length(); i++) {  
  11.             char c = str.charAt(i);  
  12.             if(Character.isUpperCase(c)) {  
  13.                 upCount++;  
  14.             } else if(Character.isLowerCase(c)) {  
  15.                 lowCount++;  
  16.             } else {  
  17.                 otherCount++;     
  18.             }  
  19.         }  
  20.         System.out.println("大写字母个数:" + upCount);  
  21.         System.out.println("小写字母个数:" + lowCount);  
  22.         System.out.println("其他字母个数:" + otherCount);  
  23.     }     
  24. }  


方法三:

 

[java] view plain copy
 
  1. //方法三:先定义两个字符串a到z和A到Z,再逐个取出str字符串中的每个字母,  
  2. //用indexOf()方法来判断字符是否在这这个定义的字符串中,在大写字母这一行,  
  3. //大写字母的计数器就加1,在小写字母这行,小写字母就加一,否则其他字母计算器  
  4. //加1  
  5.   
  6. class FindLetter2 {  
  7.     public static void main(String[] args) {  
  8.         String low = "abcdefghijklmnopqrstuvwxyz";  
  9.         String up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  10.         int lowCount = 0;  
  11.         int upCount = 0;  
  12.         int otherCount = 0;  
  13.         String str = "Helle, This is A test textfile.123456, tannk you!!";  
  14.           
  15.         for(int i = 0; i < str.length(); i++) {  
  16.             char c = str.charAt(i);  
  17.             if(low.indexOf(c) != -1) {  
  18.                 lowCount++;  
  19.             } else if(up.indexOf(c) != -1) {  
  20.                 upCount++;  
  21.             } else {  
  22.                 otherCount++;     
  23.             }  
  24.         }  
  25.         System.out.println("大写字母个数:" + upCount);  
  26.         System.out.println("小写字母个数:" + lowCount);  
  27.         System.out.println("其他字母个数:" + otherCount);  
  28.     }     
  29. }  


方法四:

 

[java] view plain copy
 
  1. //把str分别转化为大写和小写 大写用sU 小写 sL  
  2. //然后通过与原串比较来统计个数  
  3.   
  4. class FindLetter3 {  
  5.     public static void main(String[] args) {  
  6.         String str = "Helle, This is A test textfile.123456, tannk you!!";    
  7.         String sU = str.toUpperCase();  
  8.         String sL = str.toLowerCase();  
  9.         int lowCount = 0;  
  10.         int upCount = 0;  
  11.         int otherCount = 0;  
  12.         for(int i = 0; i < str.length(); i++) {  
  13.             char charSTR = str.charAt(i);  
  14.             char charSU = sU.charAt(i);  
  15.             char charSL = sL.charAt(i);  
  16.               
  17.             //如果不是字母,是其他字符,则直接用otherCount来计数  
  18.             if(Character.isLetter(charSTR)) {  
  19.             //如果原串与转换过后的大写字母串相等,则原来字符为大写字母,  
  20.             //若与小写字母相等,则为小写字母  
  21.                 if( charSTR == charSU) {      
  22.                     upCount++;  
  23.                 } else if(charSTR == charSL) {  
  24.                     lowCount++;  
  25.                 }  
  26.             } else {  
  27.                 otherCount++;     
  28.             }  
  29.         }  
  30.           
  31.         System.out.println("大写字母个数:" + upCount);  
  32.         System.out.println("小写字母个数:" + lowCount);  
  33.         System.out.println("其他字母个数:" + otherCount);  
  34.     }     
  35. }  


 这四种算法都有正确的输出:

大写字母个数:3
小写字母个数:29
其他字母个数:18