Java program to find the percentage of uppercase, lowercase, digits and special characters in a String

Convert the given string into an array of characters for each character of the array verify whether it is upper case, lower case, digit or, any other character using isUpperCase(), isLowerCase(), isDigit() method of the Character class.


Live Demo

public class Sample2 {
   public static void main(String args[]) {
      String data = "Hello HOW are you MR 51";
      char [] charArray = data.toCharArray();
      int upper = 0;
      int lower = 0;
      int digit = 0;
      int others = 0;

      int totalChars = data.length();
      for(int i=0; i<data.length(); i++) {
         if (Character.isUpperCase(charArray[i])) {
         } else if(Character.isLowerCase(charArray[i])) {
         } else if(Character.isDigit(charArray[i])){
         } else {
      System.out.println("Total length of the string :"+totalChars);
      System.out.println("Upper case :"+upper);
      System.out.println("Percentage of upper case letters: "+(upper*100)/totalChars);
      System.out.println("Lower case :"+lower);
      System.out.println("Percentage of lower case letters:"+(lower*100)/totalChars);
      System.out.println("Digit :"+digit);
      System.out.println("Percentage of digits :"+(digit*100)/totalChars);
      System.out.println("Others :"+others);
      System.out.println("Percentage of other characters :"+(others*100)/totalChars);


Total length of the string :23
Upper case :6
Percentage of upper case letters: 26
Lower case :10
Percentage of lower case letters:43
Digit :2
Percentage of digits :8
Others :5
Percentage of other characters :21