統計大寫字母個數

題目描述

找出給定字符串中大寫字符(即'A'-'Z')的個數
接口說明
    原型:int calcCapital(String str);
    返回值:int

輸入描述

輸入一個String數據

輸出描述

輸出string中大寫字母的個數

輸入例子

add123#$%#%#O

輸出例子

1

算法實現

import java.util.Scanner;

/**
 * All Rights Reserved !!!
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNext()) {
            String input = scanner.nextLine();
            System.out.println(count(input));
        }

        scanner.close();
    }

    private static int count(String s) {
        int result = 0;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                result++;
            }
        }

        return result;
    }
}
相關文章
相關標籤/搜索