標籤:分析字串 方法 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
- //方法一:在利用每個字元的Unicode碼在a~z之間,調用jdk提
- //供的String類的charAt取出字串每一個字元,逐個進行比較來判定
-
- class FindLetter {
- public static void main(String[] args) {
- String str = "Helle, This is A test textfile.123456, tannk you!!";
- int upCount = 0;
- int lowCount = 0;
- int otherCount = 0;
-
- for(int i = 0; i < str.length(); i++) {
- char c = str.charAt(i);
- if(c >= ‘a‘ && c <= ‘z‘) {
- lowCount++;
- } else if(c >= ‘A‘ && c <= ‘Z‘) {
- upCount++;
- } else {
- otherCount++;
- }
- }
- System.out.println("大寫之母個數:" + upCount);
- System.out.println("小寫字母個數:" + lowCount);
- System.out.println("其他字元個數:" + otherCount);
- }
- }
方法二:
[java] view plain copy
- //方法二:用jdk的Character類的isUpperCase方法和isLowerCase方法
-
- class FindLetter1 {
- public static void main(String[] args) {
- String str = "Helle, This is A test textfile.123456, tannk you!!";
- int upCount = 0;
- int lowCount = 0;
- int otherCount = 0;
-
- for(int i = 0; i < str.length(); i++) {
- char c = str.charAt(i);
- if(Character.isUpperCase(c)) {
- upCount++;
- } else if(Character.isLowerCase(c)) {
- lowCount++;
- } else {
- otherCount++;
- }
- }
- System.out.println("大寫字母個數:" + upCount);
- System.out.println("小寫字母個數:" + lowCount);
- System.out.println("其他字母個數:" + otherCount);
- }
- }
方法三:
[java] view plain copy
- //方法三:先定義兩個字串a到z和A到Z,再逐個取出str字串中的每個字母,
- //用indexOf()方法來判斷字元是否在這這個定義的字串中,在大寫字母這一行,
- //大寫字母的計數器就加1,在小寫字母這行,小寫字母就加一,否則其他字母計算機
- //加1
-
- class FindLetter2 {
- public static void main(String[] args) {
- String low = "abcdefghijklmnopqrstuvwxyz";
- String up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int lowCount = 0;
- int upCount = 0;
- int otherCount = 0;
- String str = "Helle, This is A test textfile.123456, tannk you!!";
-
- for(int i = 0; i < str.length(); i++) {
- char c = str.charAt(i);
- if(low.indexOf(c) != -1) {
- lowCount++;
- } else if(up.indexOf(c) != -1) {
- upCount++;
- } else {
- otherCount++;
- }
- }
- System.out.println("大寫字母個數:" + upCount);
- System.out.println("小寫字母個數:" + lowCount);
- System.out.println("其他字母個數:" + otherCount);
- }
- }
方法四:
[java] view plain copy
- //把str分別轉化為大寫和小寫 大寫用sU 小寫 sL
- //然後通過與原串比較來統計個數
-
- class FindLetter3 {
- public static void main(String[] args) {
- String str = "Helle, This is A test textfile.123456, tannk you!!";
- String sU = str.toUpperCase();
- String sL = str.toLowerCase();
- int lowCount = 0;
- int upCount = 0;
- int otherCount = 0;
- for(int i = 0; i < str.length(); i++) {
- char charSTR = str.charAt(i);
- char charSU = sU.charAt(i);
- char charSL = sL.charAt(i);
-
- //如果不是字母,是其他字元,則直接用otherCount來計數
- if(Character.isLetter(charSTR)) {
- //如果原串與轉換過後的大寫字母串相等,則原來字元為大寫字母,
- //若與小寫字母相等,則為小寫字母
- if( charSTR == charSU) {
- upCount++;
- } else if(charSTR == charSL) {
- lowCount++;
- }
- } else {
- otherCount++;
- }
- }
-
- System.out.println("大寫字母個數:" + upCount);
- System.out.println("小寫字母個數:" + lowCount);
- System.out.println("其他字母個數:" + otherCount);
- }
- }
這四種演算法都有正確的輸出:
大寫字母個數:3
小寫字母個數:29
其他字母個數:18
在一個字串中,統計大寫字母個數,小寫字母個數,其他字元個數的四種演算法