# Calculate number & percentage of characters in a String

By | September 16, 2017

This is a program to calculate number and percentage of Lowercase letters, uppercase letters, digits and other special characters in a string.

package Exercise4;

public class Exercise4 {
public static void main(String[] args) {
String str = "This is a Text To Show !! With @123";
int totalchar = str.length();
int UpparCase = 0;
int LowerCase = 0;
int Digits = 0;
int Other = 0;
for (int i = 0; i &lt; str.length(); i++) {
char ch = str.charAt(i);

if (Character.isUpperCase(ch)) {
UpparCase++;
} else if (Character.isLowerCase(ch)) {
LowerCase++;
} else if (Character.isDigit(ch)) {
Digits++;
} else {
Other++;
}
}

double UpparCasePercentage = (UpparCase * 100.0)/ totalchar;
double LowerCasePercentage = (LowerCase * 100.0)/ totalchar;
double DigitsPercentage = (Digits * 100.0)/ totalchar;
double OtherPercentage = (Other * 100.0)/ totalchar;

System.out.printf("Uppar Case Percentage: %.2f",UpparCasePercentage);
System.out.println();
System.out.printf("Lower Case Percentage: %.2f",LowerCasePercentage);
System.out.println();
System.out.printf("Digits Percentage: %.2f",DigitsPercentage);
System.out.println();
System.out.printf("Other Percentage: %.2f",OtherPercentage);

}
}

#### Output

Uppar Case Percentage: 14.29
Lower Case Percentage: 45.71
Digits Percentage: 8.57
Other Percentage: 31.43