-5

My question is simple: How to check if a String is more then 60% uppercase?

Bammerbom
  • 43
  • 4

7 Answers7

4

Iterate through the string and count every uppercase character. Then divide this value by the string length.

adjan
  • 13,371
  • 2
  • 31
  • 48
  • This is the generally correct approach, although AIUI determining "uppercase" can get gnarly if you move outside common alphabets. – Duncan Jones Jun 23 '14 at 13:03
  • 1
    What if the string has spaces? What if it has non-latin-alphabetic characters? – blgt Jun 23 '14 at 13:03
  • 1
    You may need to also count the lowercase characters, depending on how you define 60%. Is it 60% of all characters, or 60% of alpha characters. If the latter, then the percentage would be #upper / (#upper + #lower) – Steven Mastandrea Jun 23 '14 at 13:03
  • @blgt The person writing the code needs to decide - or have clarified - the rules for themselves at that point. They aren't covered in the question (because it's pretty low quality). – Anthony Grist Jun 23 '14 at 13:04
  • this answer is as accurate as question is, correct approach – Kasper Ziemianek Jun 23 '14 at 13:06
2

Try:

int uppers = 0;
for(char c : s.toCharArray()) {
    if(Character.isUpperCase(c)) {
        ++uppers;
    }
}
double pct = (uppers * 1D) / (s.length() * 1D) * 100D;
if(pct > 60D) {
   // do somnething
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

Something like that should do it. Not tested at all.

public boolean getPercentUpperCase(String str, int morethenpercent) {    
    int lowercaseChars = 0, uppercaseChars = 0;

    for (int i = 0; i >= str.length() -1; i++) {
        if (Character.isUpperCase(str.charAt(i))) {
            uppercaseChars++;
        } else {
            lowercaseChars++;
        }
    }

    return (Math.round(uppercaseChars / lowercaseChars) > morethenpercent);
}
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • You can improve that using a `while` which will stop when `uppercaseChars` is greater than `str.length * 0.6` (and also can stop after iterrating over all). And you do not need lowerCaseChars since lowercasechars should be considerated like `str.length - uppercaseChars`. – lpratlong Jun 23 '14 at 13:10
  • Im not sure if it's really an improvement calculating every time if it's higher then, instead of counting if it's an uppercase – Emanuel Jun 23 '14 at 13:11
  • It will be an improvement. But insignificant since, I think, its String will not be a giant String. – lpratlong Jun 23 '14 at 13:12
  • But, why `i--` ? It makes no sense! – lpratlong Jun 23 '14 at 13:13
  • doesnt matter if increasing or decreasing. anywy, changed it. – Emanuel Jun 23 '14 at 13:14
0

Try this

String test="AvCFrB";
char[] arr = test.toCharArray();
int count = 0;
for (int i = 0; i < arr.length; i++) {
    boolean upperCase = Character.isUpperCase(arr[i]);
    System.out.println(arr[i]+"="+upperCase);

    if(upperCase)
        count++;

}

double percentage = (count*1.0/test.length()*1.0)*100;
System.out.println(percentage);

Output-

A=true
v=false
C=true
F=true
r=false
B=true
66.66666666666666
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

It is completely based on programming and maths. It will be better you program this your self. However you can follow the below process.

  1. get String size
  2. check all the characters of string
  3. count each upper case
  4. apply the formula (100*total_uppercharecters)/string _size
Krutik
  • 233
  • 2
  • 4
  • 13
0

I give my solution right here which looks like some other but with some improvement I think. You can try my solution here: https://ideone.com/KGmMDa .

Note that I use while loop since I do not like break:

int upCount = 0;
int index = 0;
double percentLimit = str.length() * 0.6;
boolean upper = false;
while (!upper && (index < str.length())) {
    if(Character.isUpperCase(str.charAt(index))) {
        upCount++;
        upper = upCount >= percentLimit;
    }
    index++;
}
return upper;

Grrr, here is with for and break, but I hate break :(

int upCount = 0;
double percentLimit = str.length() * 0.6;
for (char c : str.toCharArray()) {
    if(Character.isUpperCase(c)) {
        upCount++;
        if (upCount >= percentLimit) {
            break;
        }
    }
}
return (upCount >= percentLimit);
lpratlong
  • 1,421
  • 9
  • 17
0

You could also use the stream API in java 8 to achieve this.

public class Test { 
    public static void main(String[] args){
        System.out.println(is60PercentUpper("aBc GH")); //true
    }

    public static boolean is60PercentUpper(String s){
    return s.chars()
            .filter(Character::isLetter)
            .map(entry -> Character.isUpperCase(entry) ? 1 : 0)
            .summaryStatistics()
            .getAverage() >= 0.60;
    }
}

What it does is:

  • get a stream of chars from the string
  • filter each char to only get letters
  • map each char value to 1 if its in uppercase otherwise 0
  • get the summary statistics and check if the average is superior or equals to 60%
Community
  • 1
  • 1
user2336315
  • 15,697
  • 10
  • 46
  • 64