Uppercase and lowercase counts in a string in Java
This article shows how to count the number of instances of uppercase and lowercase letters in a string by encoding our program or with java 8 and the guava API from google.isUpperCase() and isLowerCase() in Java
This method consists of going through the characters of the sentence and seeing for each one whether it is lowercase or uppercase. We need to create a counter for uppercase letters and another for lowercase letters. At the end, we display the values of the two variables.To check whether the character is lowercase or uppercase, java contains two methods:
boolean isUpperCase(char ch): This method returns true if the character is uppercase. A character is uppercase if its category is returned by Character.getType(ch) is UPPERCASE_LETTER.
boolean isLowerCase(char ch): This method returns true if the character is lowercase. A character is lowercase if its category is returned by Character.getType(ch) is LOWERCASE_LETTER.
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
You can create the recursive version of both methods nbr_min() and nbr_maj(). This time we decided to program the two methods ourselves provided by java isUpperCase() and isLowerCase(), based on the ASCII code range of the lowercase and uppercase alphabet.
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;
}
Java 8
With Java 8, you can calculate the number of uppercase and lowercase letters. We're going to call the method chars() which will return a IntStream And from there, we're going to filter out uppercase and lowercase letters by passing a filter. In the end, we're going to use the operation count() to find the number of instances.
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();
}
Google Guava
The Guava API is also used to determine the total number of uppercase and lowercase letters in a String. You can consider: ChartMatcher as a set of characters, so when you use ChartMatcher.JAVAUPPERCASE it means any uppercase letter. The Method rainFrom() will remove all non-uppercase characters.
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();
}