English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To calculate the number of uppercase and lowercase characters, we must first determine whether the given character in the string is uppercase or lowercase. For this purpose, we will use the concept of each character's ASCII value in Java.
In Java, we know that each character has a corresponding ASCII value, so we will compare each character to see if it is in the uppercase or lowercase range. In the following example, the string is first converted to a character array for horizontal use, then the character is found to be in the uppercase or lowercase letter range, and counters for uppercase and lowercase letters are also set, and the character position is increased accordingly.
public class CountUpperLower { public static void main(String[] args) { String str1 = "AbRtt"; int upperCase = 0; int lowerCase = 0; char[] ch = str1.toCharArray(); for(char chh : ch) { if(chh >='A' && chh <='Z') { upperCase++; } else if (chh >= 'a' && chh <= 'z') { lowerCase++; } else { continue; } } System.out.println("Count of Uppercase letter/s is/are " + upperCase + " and of Lowercase letter/s is/are " + lowerCase); } }
Output Result
Count of Uppercase letter/s is/are 2 and of Lowercase letter/s is/are 3