统计字符串中大写字母个数


可将字符串转为字符数组,然后对数组进行遍历,进而统计大写字母的个数。

下面给出代码:

import java.util.Scanner;

public class Main {
  public static void main(String args[]){
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();
    int count = 0;
    char ch[] = str.toCharArray();  //转为字符数组
    for(int i = 0;i<ch.length;i++){
      if(ch[i]>='A'&&ch[i]<='Z'){
      count++;  //遍历数组,进行统计
    }
  }
    System.out.println(count);
  }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM