0

I am trying to print the output of a String to find the count of uppercase and lowercase in it.

E.g. if string = "AaaBBbCc", I need the output as : "A1a2B2b1C1c1".

I.E. count of uppercase 'A' then count of lowercase 'a', appending with the characters.

Below is the code snippet till where I have done. Can any one suggest how it goes. I know code is not up-to the mark :(

public static void main(String[] args) {
    String str = "AaaBBbCc";
    int upperCount=0;
    int lowerCount=0;

    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if(ch>='A' && ch<='Z'){
             upperCount++;
             System.out.println("Uppercase letter is : "+ch+upperCount);

    }
     if(ch>='a' && ch<='z'){
        lowerCount++;
        System.out.println("Lower case letter is : "+ch+lowerCount);
    }
}
    System.out.println("upper count is :"+upperCount+" & lower count is: "+lowerCount);     

}

MrPublic
  • 520
  • 5
  • 16
  • Are all of the equal letters always next to each other, like in your example? Will there ever be non-letter characters in the string? – Andy Turner Mar 14 '16 at 11:42
  • [`if (Character.isUpperCase(ch))`](https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isUpperCase%28char%29) / [`if (Character.isLowerCase(ch))`](https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLowerCase%28char%29) – khelwood Mar 14 '16 at 11:42
  • @AndyTurner Ah, OK, I looked more closely at the code and skipped the text. – Johannes Jander Mar 14 '16 at 11:47

2 Answers2

0

You are on the right track. If you want to count which letters appear, not only if they are upper or lower case, you could create 2 int[] arrays upperCaseCount and lowerCaseCount = new int[26]. You could use these arrays to keep count of which letters are appearing.

Hint you can exploit the fact that char can be used as int to determine which index you should increment:

int index = ? //'a' should be 0 for lower, and 'A' should be 0 for upper
lowerCaseCount[index]++ or upperCaseCount[index]++; 
Maljam
  • 6,244
  • 3
  • 17
  • 30
0

What you are trying to accomplish here is called Run-length encoding. This is sometimes referred to as a form of lossless data compression in which the length of a continuous character is appended to a single instance of that character. Here is a modified version from RosettaCode that should do the trick for you:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RunLengthEncoding {

    public static String encode(String source) {
        StringBuffer dest = new StringBuffer();
        for (int i = 0; i < source.length(); i++) {
            int runLength = 1;
            while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) {
                runLength++;
                i++;
            }
            /* We will swap these so they fit your format of [Letter][Count]
            dest.append(runLength);
            dest.append(source.charAt(i));
            */
            dest.append(source.charAt(i));
            dest.append(runLength);
        }
        return dest.toString();
    }

    public static void main(String[] args) {
        String example = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
        System.out.println(encode(example));
    }
}
MrPublic
  • 520
  • 5
  • 16
  • What will happen if the input string is "AaBBAAAaCac" ?? I think the ans should be A4a3B2C1c1, isn't it? – Ataur Rahman Munna Mar 14 '16 at 12:22
  • @AtaurRahmanMunna Original question seems a bit unclear as to whether they want just the count of all uppercase and lowercase letters (Because of `System.out.println("upper count is :"+upperCount+" & lower count is: "+lowerCount);`), something exactly like run-length encoding (Because of "E.g. if string = "AaaBBbCc", I need the output as : "A1a2B2b1C1c1"."), or something along the lines of what you said where the output is alphabetized with each character count appearing after the letter (From what you said). – MrPublic Mar 14 '16 at 12:28