下面用了三种算法实现,执行效率逐渐提高...
第一种:
public class TestString { public static void main(String[] args) { String s = "AdaaaaABBBBdkajfdkfj4dasdWWSDS53#$#addasdfkjsakfls_kjdksADKDJ"; int lCount = 0, uCount = 0, oCount = 0; for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(c >= 'a' && c <= 'z') { lCount++; } else if(c >= 'A' && c <= 'Z') { uCount++; } else { oCount++; } } System.out.println(lCount + " " + uCount + " " + oCount); } }第二种:
public class TestString { public static void main(String[] args) { String s = "AdaaaaABBBBdkajfdkfj4dasdWWSDS53#$#addasdfkjsakfls_kjdksADKDJ"; int lCount = 0, uCount = 0, oCount = 0; String sL = "abcdefghijklmnopqrstuvwxyz"; String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i = 0; i <s.length(); i++) { char c = s.charAt(i); if(sL.indexOf(c) != -1) { lCount++; } else if(sU.indexOf(c) != -1) { uCount++; } else { oCount++; } } System.out.println(lCount + " " + uCount + " " + oCount); } }public class TestString { public static void main(String[] args) { String s = "AdaaaaABBBBdkajfdkfj4dasdWWSDS53#$#addasdfkjsakfls_kjdksADKDJ"; int lCount = 0, uCount = 0, oCount = 0; for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(Character.isLowerCase(c)) { lCount++; } else if(Character.isUpperCase(c)) { uCount++; } else { oCount++; } } System.out.println(lCount + " " + uCount + " " + oCount); } }
第三种: