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

方法一:

public class Test3{
 public static void main(String[] args){
  String s = "kasdassadsaSJKFJKSDKF823423445676$^&%^&";
  int lc=0,uc=0,others=0;
 
  for(int i=0;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   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   char c = s.charAt(i);
   if(Character.isLowerCase(c))
   lc++;
   else if(Character.isUpperCase(c))
   uc++;
   else
   others++;
   }
   System.out.println(lc+" "+uc+" "+others);
  }
 }

你可能感兴趣的:(输出一个字符串中的小写字母、大写字母、其他字符的个数)