Find Percentage of Uppercase, Lowercase, Digits, and Special Characters in a String



This article will teach us how to transform a string into an array of characters in order to examine its composition. Next, we shall categorize every character as a numeric, special character, lowercase letter, or uppercase letter. The methods of Java Character class will be used to do this. For each group (uppercase, lowercase, numbers, and special characters), we'll compute the percentage and show the results. 

isUpperCase() : The Java Character isUpperCase() method determines if a character is an uppercase character or not.

isLowerCase() : The Java Character isLowerCase() method determines if a character is a lowercase character or not.

isDigit() : The Java Character isDigit() method is used to check whether the specified character is a digit or not.

Problem Statement

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.

Input

Hello HOW are you MR 51

Output

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

Steps to find the percentage

Following are the to find the percentage of uppercase, lowercase, digits, and special characters in a String ?

  • Initialize a class
  • In the main method define a string and convert it to a character array.
  • Create counters for uppercase, lowercase, digits, and special characters.
  • Get the length of the string using length() method .
  • Loop through the array using the for loop, updating counters using Character.isUpperCase(), Character.isLowerCase(), and Character.isDigit() methods.
  • Calculate percentages for each character type.
  • Display the total length, counts, and percentages of each character type.

Java program to find the percentage

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

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])) {
            upper++;
         } else if(Character.isLowerCase(charArray[i])) {
            lower++;
         } else if(Character.isDigit(charArray[i])){
            digit++;
         } else {
            others++;
         }
      }
      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);
   }
}

Output

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

Code Explanation

In the program given above first, we will define the Sample2 class in the Java program that is provided. We will be using the data.toCharArray(), we transform the string data into a character array inside the main method. The counters for capital, lowercase, digit, and other characters are then initialized. We use Character.isUpperCase(), Character.isLowerCase(), and Character.isDigit() to check the type of each character using a for loop. By the outcomes, we increase the corresponding counters. 

Updated on: 2024-08-22T12:11:06+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements