Java program to count upper and lower case characters in a given string

Java 8Object Oriented ProgrammingProgramming

In order to count upper and lower case character we have to first find weather a given character is in upper case or in lower case.For this we would take concept of ASCII value of each character in Java.

In Java as we know for each character has corresponding ASCII value so we would compare each character that it lies in the range of upper case or in lower case.In below example first convert string to a character array for easy transverse,then find weather it lies in upper case or lower case range also setted counter for upper case and lower case which get increased as character lies accordingly.


 Live Demo

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') {
         } else if (chh >= 'a' && chh <= 'z') {
         } else {
      System.out.println("Count of Uppercase letter/s is/are " + upperCase + " and of Lowercase       letter/s is/are " + lowerCase);


Count of Uppercase letter/s is/are 2 and of Lowercase letter/s is/are 3
Published on 01-Nov-2018 13:58:05