
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
- Related Questions & Answers
- Java program to convert a string to lowercase and uppercase.
- Converting Odd and Even-indexed characters in a string to uppercase/lowercase in JavaScript?
- Replacing upperCase and LowerCase in a string - JavaScript
- Count Uppercase, Lowercase, special character and numeric values in C++
- Golang Program to convert Uppercase to Lowercase characters, using binary operator.
- Making a Java String All Uppercase or All Lowercase.
- Return element-wise a copy of the string with uppercase characters converted to lowercase and vice versa in Numpy
- Python Program to Count Number of Lowercase Characters in a String
- Convert string to lowercase or uppercase in Arduino
- C Program for LowerCase to UpperCase and vice-versa
- Count Number of Lowercase Characters in a String in Python Program
- Check if lowercase and uppercase characters are in same order in Python
- Java Program to Find the Duplicate Characters in a String
- Java Program to find duplicate characters in a String?
- Conversion of whole String to uppercase or lowercase using STL in C++
Advertisements