输出一个字符串中的小写字母、大写字母、其他字符的个数

本文提供了三种不同的Java方法来统计一个给定字符串中的小写字母、大写字母和其他字符的数量。这些方法包括使用ASCII值范围检查、字符串匹配以及Java内置的Character类方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:

public class Test3{
 public static void main(String[] args){
  String s = "kasdassadsaSJKFJKSDKF823423445676$^&%^&";
  int lc=0,uc=0,others=0;
 
  for(int i=0;i<s.length();i++){
   char c = s.charAt(i);
   if(c>='a'&&c<='z')
   lc++;
   else if(c>='A'&&c<='Z')
   uc++;
   else
   others++;
   
   }
   System.out.println(lc+" "+uc+" "+others);
  }
 }

 

方法二:

public class Test4{
 public static void main(String[] args){
  String s = "sjajdjasdASDASASD42342342";
  int lc =0,uc=0,others=0;
  String lcase="abcdefghijklmnopqrstuvwxyz";
  String ucase="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for(int i=0;i<s.length();i++){
   char c = s.charAt(i);
   if(lcase.indexOf(c)!=-1)
   lc++;
   else if(ucase.indexOf(c)!=-1)
   uc++;
   else
   others++;
   }
   System.out.println(lc+" "+uc+" "+others);
  }
 }

 

方法三:

public class Test5{
 public static void main(String[] args){
  String s ="sdjasdjaSAJDASDLADL6743247382";
  int lc=0,uc=0,others=0;
  for(int i=0;i<s.length();i++){
   char c = s.charAt(i);
   if(Character.isLowerCase(c))
   lc++;
   else if(Character.isUpperCase(c))
   uc++;
   else
   others++;
   }
   System.out.println(lc+" "+uc+" "+others);
  }
 }

### 回答1: 可以使用以下源码来判断字符串大写字母小写字母个数:int upperCount = 0; int lowerCount = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (Character.isUpperCase(ch)) { upperCount++; } else if (Character.isLowerCase(ch)) { lowerCount++; } } ### 回答2: 下面是一个使用Python语言编写的函数,用于判断一个字符串大写字母小写字母个数。 ```python def count_upper_lower(string): upper_count = 0 lower_count = 0 for char in string: if char.isupper(): upper_count += 1 elif char.islower(): lower_count += 1 return upper_count, lower_count ``` 使用示例: ```python s = "AbCdefG" count = count_upper_lower(s) print("大写字母个数:", count[0]) print("小写字母个数:", count[1]) ``` 输出结果: ``` 大写字母个数: 3 小写字母个数: 4 ``` 这个函数首先初始化了两个计数变量`upper_count`和`lower_count`,然后遍历输入字符串中的每个字符。使用字符对象的`isupper()`方法判断字符是否为大写字母,如果是,则将`upper_count`加一;使用字符对象的`islower()`方法判断字符是否为小写字母,如果是,则将`lower_count`加一。最后,函数返回元组`(upper_count, lower_count)`,其中第一个元素表示大写字母个数,第二个元素表示小写字母个数。 ### 回答3: 下面是一个用Python编写的判断一个字符串大写字母小写字母个数的源码示例: ```python def count_upper_lower(string): upper_count = 0 lower_count = 0 # 遍历字符串中的每个字符 for char in string: # 判断是否为大写字母,是则计数器加一 if char.isupper(): upper_count += 1 # 判断是否为小写字母,是则计数器加一 elif char.islower(): lower_count += 1 return upper_count, lower_count # 测试示例 string = "Hello World" upper, lower = count_upper_lower(string) print("大写字母个数:", upper) print("小写字母个数:", lower) ``` 在该示例中,首先定义了一个`count_upper_lower`函数,该函数接受一个字符串作为参数。然后,使用两个计数器变量`upper_count`和`lower_count`来记录大写字母小写字母个数。 接下来,通过遍历字符串中的每个字符,使用字符串的`isupper()`和`islower()`方法判断字符是否为大写字母小写字母,如果是,则对应计数器加一。 最后,将计数器的值作为元组的形式返回。在主程序中,我们测试了一个字符串"Hello World"的结果,并分别使用`print`函数输出大写字母小写字母个数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值