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

本文介绍两种方法来统计字符串中的大写字母、小写字母和数字字符的数量。方法一利用ASCII码范围直接计数,方法二通过字符大小写转换对比实现计数。

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

示例: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);	 
	}
}

日常鸡汤:有一种落差是

                  你配不上自己的野心

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

以下是一个可以统输入字符串小写字母大写字母数字各自数量的C语言程序。该程序的思路是遍历输入的字符串,根据字符的ASCII码范围判断其属于大写字母小写字母还是数字,然后对应计数器加1。 ```c #include <stdio.h> #include <string.h> // 统函数 void count_characters(char *str, int *uppercase, int *lowercase, int *numbers) { *uppercase = 0; *lowercase = 0; *numbers = 0; for (int i = 0; i < strlen(str); i++) { if (str[i] >= 'A' && str[i] <= 'Z') { (*uppercase)++; } else if (str[i] >= 'a' && str[i] <= 'z') { (*lowercase)++; } else if (str[i] >= '0' && str[i] <= '9') { (*numbers)++; } } } int main() { char str[1024]; int uppercase_count, lowercase_count, number_count; printf("请输入字符串:\n"); // 读取输入的字符串 fgets(str, sizeof(str), stdin); // 去除fgets可能读取的换行符 str[strcspn(str, "\n")] = 0; // 调用统函数 count_characters(str, &uppercase_count, &lowercase_count, &number_count); // 输出结果 printf("大写字母个数: %d\n", uppercase_count); printf("小写字母个数: %d\n", lowercase_count); printf("数字个数: %d\n", number_count); return 0; } ``` ### 代码解释 1. **`count_characters`函数**:接收一个字符串指针和三个整数指针,分别用于存储大写字母小写字母数字的数量。通过遍历字符串,根据字符的范围判断其类型并增加相应的计数器。 2. **`main`函数**:声明一个字符数组用于存储输入的字符串,以及三个整数变量用于存储统结果。使用`fgets`函数读取用户输入的字符串,并去除可能的换行符。调用`count_characters`函数进行统,并输出结果。 ### 复杂度分析 - **时间复杂度**:$O(n)$,其中$n$是字符串的长度,因为需要遍历字符串一次。 - **空间复杂度**:$O(1)$,只使用了常数级的额外空间。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值