- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
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
Advertisements