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


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.

Example

 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') {
            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

Count of Uppercase letter/s is/are 2 and of Lowercase letter/s is/are 3

Updated on: 25-Jun-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements