题目描述
描述
找出给定字符串中大写字符(即’A’-‘Z’)的个数。
数据范围:字符串长度:1\le |s|\le 250\1≤∣s∣≤250
字符串中可能包含空格或其他字符
进阶:时间复杂度:O(n)\O(n) ,空间复杂度:O(n)\O(n)
输入描述:
本题含有多组样例输入
对于每组样例,输入一行,代表待统计的字符串
输出描述:
对于每组样例,输出一个整数,代表字符串中大写字母的个数
解答
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String str=sc.nextLine();
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='A'&&str.charAt(i)<='Z'){
count++;
}
}
System.out.println(count);
}
}
}