public class nombre_majuscule_minuscule {The execution of this code:
public static void main(String[] args) {
String string="Java is a programming language"+
"object-oriented created by Sun Microsystems";
int nbr_min = nbr_min(string);
int nbr_maj = nbr_maj(string);
System.out.println("number of lowercase "+nbr_min);
System.out.println("uppercase number "+nbr_maj);
}
private static int nbr_maj(String string) {
int counter=0;
for(int i = 0; i< string.length(); i++){
char ch = string.charAt(i);
if(Character.isLowerCase(ch))
counter++;
}
return counter;
}
private static int nbr_min(String string) {
int counter=0;
for(int i = 0; i< string.length(); i++){
char ch = string.charAt(i);
if(Character.isUpperCase(ch))
counter++;
}
return counter;
}
}
lowercase number 4
uppercase number 68
private static int nbr_maj_recursive(String string, int i) {The Method Declaration nbr_min_recursive() changes only at the level of the character type check that is done with the method isLowercase().
/*if i reaches the size of the string
* return 0
*/
if(string.length()-i==0)
return 0;
/*otherwise we check the next character*/
else{
char ch = string.charAt(i);
if(estUpper(ch))
/*increment i and count one
*uppercase letter*/
return nbr_maj(string, ++i)+1;
}
return nbr_maj(string, ++i);
}
private static int nbr_min_recursive(String string, int i) {The Method isUppercase() is equivalent to the isUpperCase().
if(string.length()-i==0)
return 0;
else{
char ch = string.charAt(i);
if(isLowercase(ch))
return nbr_min(string, ++i)+1;
}
return nbr_min(string, ++i);
}
static boolean isUpperCase(char ch){The Method isTiny() is equivalent to the isLowerCase().
int ascii = (int) ch;
//[A.. Z]
if((ascii>=65 & & ascii<=90)
//accented letters
|| (ascii>=192 & & ascii<=223))
return true;
return false;
}
static boolean isLowercase(char ch){
int ascii = (int) ch;
//[a.. z]
if((ascii>=97 & & ascii<=122)
//accented letters
|| (ascii>=224 & & ascii<=255))
return true;
return false;
}
public void compter_majuscules_java8() {And to count lowercase, do this:
String phrase = "This is a test";
long counter = phrase.chars().filter(Character::isUpperCase).count();
}
public void compter_minuscules_java8() {
String phrase = "This is a Test";
long counter = phrase.chars().filter(Character::isLowerCase).count();
}
public void compter_majuscules_java8() {And to count lowercase, do this:
String phrase = "This is a test";
long counter = CharMatcher.JAVA_UPPER_CASE.retainFrom(phrase).length();
}
public void compter_minuscules_java8() {
String phrase = "This is a Test";
long counter = CharMatcher.JAVA_LOWER_CASE.retainFrom(phrase).length();
}
Please disable your ad blocker and refresh the window to use this website.