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

  题目描述:编写程序,输出字符串中的大写字母、小写小母和其他的个数。如有一个字符串"Helle, This is A test textfile.123456, tannk you!!",则其大写字母个数:3,小写字母个数:29,其他字符个数:18.

  这里提供了四种算法,第一种是我们比较好理解的,也属于硬编码问题,其他三种方法要借助JAVA语言的jdk提供的api。

方法一:

//方法一:在利用每个字符的Unicode码在a~z之间,调用jdk提
//供的String类的charAt取出字符串每一个字符,逐个进行比较来判定

class FindLetter {
	public static void main(String[] args) {
		String str = "Helle, This is A test textfile.123456, tannk you!!";
		int upCount = 0;
		int lowCount = 0;
		int otherCount = 0;
		
		for(int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if(c >= 'a' && c <= 'z') {
				lowCount++;
			} else if(c >= 'A' && c <= 'Z') {
				upCount++;
			} else {
				otherCount++;	
			}
		}
		System.out.println("大写之母个数:" + upCount);
		System.out.println("小写字母个数:" + lowCount);
		System.out.println("其他字符个数:" + otherCount);
	}	
}
方法二:

//方法二:用jdk的Character类的isUpperCase方法和isLowerCase方法

class FindLetter1 {
	public static void main(String[] args) {
		String str = "Helle, This is A test textfile.123456, tannk you!!";
		int upCount = 0;
		int lowCount = 0;
		int otherCount = 0;
		
		for(int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if(Character.isUpperCase(c)) {
				upCount++;
			} else if(Character.isLowerCase(c)) {
				lowCount++;
			} else {
				otherCount++;	
			}
		}
		System.out.println("大写字母个数:" + upCount);
		System.out.println("小写字母个数:" + lowCount);
		System.out.println("其他字母个数:" + otherCount);
	}	
}

方法三:

//方法三:先定义两个字符串a到z和A到Z,再逐个取出str字符串中的每个字母,
//用indexOf()方法来判断字符是否在这这个定义的字符串中,在大写字母这一行,
//大写字母的计数器就加1,在小写字母这行,小写字母就加一,否则其他字母计算器
//加1

class FindLetter2 {
	public static void main(String[] args) {
		String low = "abcdefghijklmnopqrstuvwxyz";
		String up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		int lowCount = 0;
		int upCount = 0;
		int otherCount = 0;
		String str = "Helle, This is A test textfile.123456, tannk you!!";
		
		for(int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if(low.indexOf(c) != -1) {
				lowCount++;
			} else if(up.indexOf(c) != -1) {
				upCount++;
			} else {
				otherCount++;	
			}
		}
		System.out.println("大写字母个数:" + upCount);
		System.out.println("小写字母个数:" + lowCount);
		System.out.println("其他字母个数:" + otherCount);
	}	
}

方法四:

//把str分别转化为大写和小写 大写用sU 小写 sL
//然后通过与原串比较来统计个数

class FindLetter3 {
	public static void main(String[] args) {
		String str = "Helle, This is A test textfile.123456, tannk you!!";	
		String sU = str.toUpperCase();
		String sL = str.toLowerCase();
		int lowCount = 0;
		int upCount = 0;
		int otherCount = 0;
		for(int i = 0; i < str.length(); i++) {
			char charSTR = str.charAt(i);
			char charSU = sU.charAt(i);
			char charSL = sL.charAt(i);
			
			//如果不是字母,是其他字符,则直接用otherCount来计数
			if(Character.isLetter(charSTR)) {
			//如果原串与转换过后的大写字母串相等,则原来字符为大写字母,
			//若与小写字母相等,则为小写字母
				if( charSTR == charSU) {	
					upCount++;
				} else if(charSTR == charSL) {
					lowCount++;
				}
			} else {
				otherCount++;	
			}
		}
		
		System.out.println("大写字母个数:" + upCount);
		System.out.println("小写字母个数:" + lowCount);
		System.out.println("其他字母个数:" + otherCount);
	}	
}

 这四种算法都有正确的输出:

大写字母个数:3
小写字母个数:29
其他字母个数:18



  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值