統計字符串中大寫字母個數


可將字符串轉為字符數組,然后對數組進行遍歷,進而統計大寫字母的個數。

下面給出代碼:

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