Javatpoint Logo
Javatpoint Logo

How to Find the Percentage of Uppercase Letters, Lowercase Letters, Digits, and Special Characters in a String Using Java?

In Java, analyzing the composition of a string and calculating the percentage of different character types, such as uppercase letters, lowercase letters, digits, and special characters, can be achieved by utilizing string manipulation and character classification methods. This section will guide you through the process step-by-step, enabling you to calculate the percentage of each character type in a given string.

CharacterPercentageCalculator.java

Output:

Enter a string: Hello123@123
Uppercase letters: 8.333333333333334%
Lowercase letters: 33.333333333333336%
Digits: 50.0%
Special characters: 8.333333333333334%

Explanation

We start by prompting the user to enter a string using the Scanner class and store it in the input variable.

We initialize variables to keep track of the counts for uppercase letters, lowercase letters, digits, and special characters.

We iterate through each character of the input string using a for loop.

For each character, we check its type using the Character class methods isUpperCase(), isLowerCase(), and isDigit().

Based on the character's type, we increment the respective count variables.

After counting all the characters, we calculate the percentage of each category by dividing the count by the total number of characters and multiplying by 100.

Finally, we display the calculated percentages to the user.

Conclusion:

In conclusion, using the Java code provided, you can determine the proportion of capital and lowercase letters, numbers, and special characters in a given string.

Various scenarios, including data analysis, text processing, and information validation, can benefit from its functionality. You can learn more about the properties of the data you are working with by comprehending the structure of a string. The code can also serve as a basis for more complex string analysis projects or as a building component for bigger applications.

By following the step-by-step guide and utilizing the provided code, you can easily incorporate this functionality into your Java projects. Feel free to modify and expand upon the code to suit your specific requirements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA