给出一组字符,要求输出该字符串中的小写字母个数、大写字母个数以及数字字符个数,其余字符计为一体

题目:给出一组字符,要求输出该字符串中的小写字母个数、大写字母个数以及数字字符个数,其余字符计为一体。

示例:Hello,My name is Dashuaige,I'm 12 years old.

方法一:通过对ascii码表的规律可知

  •                                                          小写字母x:          'a'=<x<='z'  (或97=<x<=122)
  •                                                          大写字母x:          'A'=<x<='Z' (或65=<x<=90 )
  •                                                          数字字符x:          'A'=<x<='Z' (或48=<x<=57 )

                可统计出字符串中的数字字符、大写字符、小写字符。

方法二:针对大小写

  •             将字符串变成字符数组
  •             然后将每个字符进行变大写,变小写,与原来的字符拿来匹配,如果不等,则发生变化,计数
  •             针对数字如方法一

                          toUpperCase()变大写
                          toLowerCase()变小写

方法一:比较实用,推荐使用。

public class lj01 {

	public static void main(String[] args) {
        方法一:原始方法
		 System.out.println("请输入一段字符串(可包含英文大小写,数字,中文以及其他符号)");
		 Scanner sc=new Scanner(System.in); 
		 String str=sc.nextLine();
		 char[] chs = str.toCharArray() ;//将字符串转换为字符数组
		 int bigCount=0;
		 int smallCount=0;
		 int numCount=0;
		 int otherCount=0;
		 for(int i=0;i<str.length();i++) {
			 if(chs[i]>='A'&& chs[i]<='Z') {
				 bigCount++;
			 }else if(chs[i]>='a'&& chs[i]<='z'){
				 smallCount++;
			 }else if(chs[i]>='0'&&chs[i]<='9') {
				 numCount++;
			 }else{
				otherCount++;
			 }
		 }
		 System.out.println("大写字母字符的个数为"+bigCount);
		 System.out.println("小写字母字符的个数为"+smallCount);
		 System.out.println("数字字符的个数为"+numCount);
		 System.out.println("其他字符的个数为"+otherCount);
        }
}

方法二:阅读参考(此种方法只针对英文字符的大小写)。

public class lj01 {

	public static void main(String[] args) {
        System.out.println("请输入一段字符串(可包含英文大小写,数字,中文以及其他符号)");
		 Scanner sc=new Scanner(System.in); 
		 String str=sc.nextLine();//原字符串
		 char[] chs = str.toCharArray() ;//将字符串转换为字符数组
		 int bigCount=0;
		 int smallCount=0;
		 String str2=str.toUpperCase();//大写字符串
		 char[] arr2 = str2.toCharArray() ;
		 for(int i=0;i<str2.length();i++) {
			if(arr2[i]==chs[i]&& arr2[i]<97+27&& arr2[i]>64) {
				bigCount++;
			}	 
		 }
		 String str3=str.toLowerCase();//大写字符串
		 char[] arr3 = str3.toCharArray() ;
		 for(int i=0;i<str3.length();i++) {
			if(arr3[i]==chs[i]&& arr3[i]<97+27&& arr3[i]>64) {
				smallCount++;
			}	 
		 }
		 System.out.println("大写字母字符有"+bigCount);
		 System.out.println("小写字母字符有"+smallCount);	 
	}
}

日常鸡汤:有一种落差是

                  你配不上自己的野心

                  也辜负了所受的苦难。。。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值