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

來源:互聯網
上載者:User

標籤:分析字串   方法   this   計數   def   直接   text   原來   nal   

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.