Count Uppercase, Lowercase, special character, and numeric values
- We can find a total number of Uppercase characters,Lowercase characters,special characters and number of digits in a given string in java by using Character class like below:
Program:
public class countDifferentCharacters {
public static void main(String[] args) {
String str = "KEEP123$%^&learn";
char[] s = str.toCharArray();
int digits = 0, letters = 0, upperCase = 0, alphabets = 0, specialCharacter = 0;
for (char ch : s) {
if (Character.isDigit(ch)) {
digits++;
}
if (Character.isLetter(ch)) {
letters++;
}
if (Character.isUpperCase(ch)) {
upperCase++;
}
if (Character.isLowerCase(ch)) {
alphabets++;
}
if (!Character.isDigit(ch) && !Character.isLetter(ch)) {
specialCharacter++;
}
}
System.out.println("digits=" + digits);
System.out.println("letters=" + letters);
System.out.println("upperCase=" + upperCase);
System.out.println("alphabets=" + alphabets);
System.out.println("specialCharacter=" + specialCharacter);
}
}
No comments:
Post a Comment
Please comment below to feedback or ask questions.