统计大写字母个数

import java.util.Scanner;

public class Main{
public static void main(String[]args){
Scanner scanner = new Scanner(System.in);
String s=scanner.nextLine();
if (s.isEmpty()) {
System.out.println(0);
}else{
int no=0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)>=65 && s.charAt(i)<=90) {
no++;
}
}
System.out.println(no);
}
}
}

还有一个方法,个人认为没问题,但是在

oj上是错误的。

import java.util.Scanner;

public class GetUpperCaseNum {
	/**
	 *
	 * 找出给定字符串中大写字符(即'A'-'Z')的个数
	 */
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String num = scan.nextLine();
		int count = 0;
		if(num.isEmpty())
			System.out.println(0);
		char[] ch = num.toCharArray();
		for (char c : ch) {
			if(Character.isUpperCase(c))
				count++;
		}
		System.out.println(count);
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 22:00:25

统计大写字母个数的相关文章

在一个字符串中,统计大写字母个数,小写字母个数,其他字符个数的四种算法

题目描述:编写程序,输出字符串中的大写字母.小写小母和其他的个数.如有一个字符串"Helle, This is A test textfile.123456, tannk you!!",则其大写字母个数:3,小写字母个数:29,其他字符个数:18. 这里提供了四种算法,第一种是我们比较好理解的,也属于硬编码问题,其他三种方法要借助JAVA语言的jdk提供的api. 方法一: <!DOCTYPE html> <html lang="en"> &

华为OJ:统计大写字母个数

这道题我错了一次,主要是A跟Z边界没有考虑,其它的就是记得測试时用nextLine,由于字符串可能可能有空格. import java.util.Scanner; public class bigLetterCount { public static int CalcCapital(String str){ int count=0; for(int i=0;i<str.length();i++){ if('A'<=str.charAt(i)&&str.charAt(i)<

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

可将字符串转为字符数组,然后对数组进行遍历,进而统计大写字母的个数. 下面给出代码: 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(); //转为字符数组 f

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

[华为OJ][算法总篇章] [华为OJ][056-统计大写字母个数] [工程下载] 题目描述 找出给定字符串中大写字符(即'A'-'Z')的个数 接口说明 原型:int calcCapital(String str); 返回值:int 输入描述 输入一个String数据 输出描述 输出string中大写字母的个数 输入例子 add123#$%#%#O 输出例子 1 算法实现 import java.util.Scanner; /** * Author: 王俊超 * Date: 2015-12-2

c语言求字符串中大写字母个数,单词个数,子串个数及一个整数是否为回文数

#include <stdio.h> #include <ctype.h> #pragma mark 统计从终端输入的字符中每个大写字母的个数.用#号作为输入结束标志 int main() { int num[26] = {0}, i; char c; while ((c = getchar())!='#') { if (isupper(c)) { num[c-65]++; } } for (int i = 0; i<26; i++) { if (num[i]) { prin

汇编语言——统计一个字符串中的大写字母、小写字母、数字和其他字符的个数,并显示

;统计字符串中大写字母.小写字母.数字.其他字符的个数DATAS SEGMENT buf db '12ADdf#gh592HKL*','$' tp1 db 0;大写字母个数 tp2 db 0;小写字母个数 tp3 db 0;数字的个数 tp4 db 0;其他字符的个数 str1 db 'the number of big is:','$' str2 db 'the number of small is:','$' str3 db 'the number of number is:','$' st

php字符串英文文本中大写字母,小写字母,空格,标点符号的个数统计

对一段英文文本的信息,统计其中大写字母,小写字母,空格,标点符号的个数 <?php$manuscript = "Where there is a will, there is a way.";//字符串文本$smallLetter = 0;$capitalLetter = 0;$blank = 0;$punctuation = 0; $num=strlen($manuscript);$arr=str_split($manuscript);//字符串分割为数组foreach($ar

c++实验5--统计输出字符串中(大/小写)字母个数,数字个数及其它字符个数。

一.问题及代码 /* * 文件名称: * 作 者: 杨楚莛 * 完成日期: 2016 年 5 月 3 日 * 版 本 号:v1.0 * 对任务及求解方法的描述部分:统计输出字符串中(大/小写)字母个数,数字个数及其它字符个数. * 输入描述: * 问题描述: * 程序输出: * 问题分析: * 算法设计: */ #include<iostream> #include<cstdio> using namespace std; int main() { char str[50]; in

大写字母出现的次数并且打印

import java.util.Scanner; /** * 2.编写一个java程序,提示用户输入一个字符串,要求字符串中必须存在字母(需要代码判断) a. 若不符合要求,则提示用户重新输入直到符合要求为止 b. 若符合要求 ,则判断字符串中的大写字母出现的次数并且打印 */public class Test02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while