在一個字符串中,統計大寫字母個數,小寫字母個數,其他字符個數的四種算法


題目描述:編寫程序,輸出字符串中的大寫字母、小寫小母和其他的個數。如有一個字符串"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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM