从字符串中分离出字符,依次将每一个字符与'a','z'比较,判断是否为小写字母,与'A','Z'比较,判断是否为大写字母,其他的为非英文字母。
方法二:
从字符串中分离出字符,依次将每一个字符在'a'~'z'中查找,如果查到则为小写字母,同样方法判断是否为大写字母,其他的为非英文字母。
方法三:
从字符串中分离出字符,依次将每一个字符用Character.isLowerCase()方法判断是否为小写字母,Character.isUpperCase()判断是否为大写字母,其他的为非英文字母。
public static void numChar1(String s) {
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<s.length() ;i++) {
c = s.charAt(i) ;
if(c > 'a' && c < 'z') {
cL ++ ;
}
else if(c > 'A' && c < 'Z') {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
public static void numChar2(String s) {
String sL = "abcdefghijklmnopqrstuvwxyz" ;
String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ; i<s.length() ;i++) {
c = s.charAt(i) ;
if(sL.indexOf(c) != -1) {
cL ++ ;
}
else if(sU.indexOf(c) != -1) {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
public static void numChar3(String s) {
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<s.length() ;i++) {
c = s.charAt(i) ;
if(Character.isLowerCase(c)) {
cL ++ ;
}
else if(Character.isUpperCase(c)) {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
}