題目描述:編寫程序,輸出字符串中的大寫字母、小寫小母和其他的個數。如有一個字符串"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>
- //方法一:在利用每個字符的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);
- }
- }
方法二:
- //方法二:用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);
- }
- }
方法三:
- //方法三:先定義兩個字符串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);
- }
- }
方法四:
- //把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
