Here you can find the source of fractionOfStringUppercase(String input)
Parameter | Description |
---|---|
input | Input string. |
public static double fractionOfStringUppercase(String input)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w ww .j a va 2s . co m*/ * Of the characters in the string that have an uppercase form, how many are uppercased? * * @param input Input string. * @return The fraction of uppercased characters, with {@code 0.0d} meaning that all uppercasable characters are in * lowercase and {@code 1.0d} that all of them are in uppercase. */ public static double fractionOfStringUppercase(String input) { if (input == null) { return 0; } double upperCasableCharacters = 0; double upperCount = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); char uc = Character.toUpperCase(c); char lc = Character.toLowerCase(c); // If both the upper and lowercase version of a character are the same, then the character has // no distinct uppercase form (e.g., a digit or punctuation). Ignore these. if (c == uc && c == lc) { continue; } upperCasableCharacters++; if (c == uc) { upperCount++; } } return upperCasableCharacters == 0 ? 0 : upperCount / upperCasableCharacters; } }