題目描述
找出給定字符串中大寫字符(即’A’-‘Z’)的個數
接口說明
原型:int CalcCapital(String str);
返回值:int
輸入描述:
輸入一個String數據
輸出描述:
輸出string中大寫字母的個數
示例
輸入
add123#$%#%#O
輸出
1
題目分析
將字符串,按字符遍歷,順便判斷該字符大小是否符合[A,Z]。
java 代碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 統計大寫字母的個數
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine()) != null){
int count = 0;
int len = line.length();
for (int i = 0; i < len; i++) {
char temp = line.charAt(i);
if(temp >= 'A' && temp <= 'Z'){
count++;
}
}
System.out.println(count);
}
}
}