题目描述
找出给定字符串中大写字符(即’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);
}
}
}