首页 > 其他 > 详细

【华为OJ】【056-统计大写字母个数】

时间:2016-05-14 10:19:37      阅读:286      评论:0      收藏:0      [点我收藏+]

【华为OJ】【算法总篇章】


【华为OJ】【056-统计大写字母个数】

【工程下载】


题目描述

找出给定字符串中大写字符(即‘A‘-‘Z‘)的个数
接口说明
    原型:int calcCapital(String str);
    返回值:int

输入描述

输入一个String数据

输出描述

输出string中大写字母的个数

输入例子

add123#$%#%#O

输出例子

1

算法实现

import java.util.Scanner;

/**
 * Author: 王俊超
 * Date: 2015-12-25 15:16
 * 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;
    }
}

【华为OJ】【056-统计大写字母个数】

原文:http://blog.csdn.net/derrantcm/article/details/51404104

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!