Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the occurrence of alphanumeric characters and printing them graphically

Tags:

java

string

I have a string, and I want to count the occurrence of all letters and numbers and want to create a graph so I can see the occurrence graphically.

So for example:

String sentence = "ABC ABC ABC 123"

A (3) * * *
B (3) * * *
C (3) * * *
D
.
.

My way of thinking:

  1. Count all numbers and letters in the string
  2. Print all asterisks times this number (unfortunately I can't multiply a String with an int in Java)

I think there are two ways of counting the characters. I can either use the charAt() method or toCharArray() and loop through the string or array and count the letters.

For example:

aCounter = 0;
bCounter = 0;
char ch = sentence.charAt(i);

for (i = 0; i < sentence.length(); ++i) {
    if (ch == 'a') {
        aCounter++;
    }
    if (ch == 'b') {
        bCounter++;
    }
}

However, I have multiple problems with this approach:

  • I would have a to make a lot of counter variables - aCounter through zCounter plus 0counter through 9counter
  • I would have to make another for loop to print the asterisks!

I'm not asking for an set answer here, I'm just looking for some good directions, because I'm stuck.

like image 835
swennemen Avatar asked Sep 12 '13 14:09

swennemen


People also ask

How do you count alphanumeric in Java?

String str = "9as78"; Now loop through the length of this string and use the Character. isLetter() method. Within that, use the charAt() method to check for each character/ number in the string.

What is alphanumeric character example?

Alphanumeric characters are the numbers 0-9 and letters A-Z (both uppercase and lowercase). An alphanumeric example are the characters a, H, 0, 5 and k.

How do you count the number of occurrences of a character in a string?

Use the count() Function to Count the Number of a Characters Occuring in a String in Python. We can count the occurrence of a value in strings using the count() function. It will return how many times the value appears in the given string. Remember, upper and lower cases are treated as different characters.

What is a collection of alphanumeric characters?

Alphanumeric, also referred to as alphameric, is a term that encompasses all of the letters and numerals in a given language set. In layouts designed for English language users, alphanumeric characters are those comprised of the combined set of the 26 alphabetic characters, A to Z, and the 10 Arabic numerals, 0 to 9.


1 Answers

There's no need to make a HashTable/HashMap/HashSet for this.

You know which characters are being tracked ahead of time, so you can use an array.

I want to count the occurrence of all letters and numbers

Make a string of the characters you will track, then initialize an array.

String sentence = "ABC ABC ABC 123";

//Make a map of all the characters you want to track.
String indexes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

//Initialize an array to the size of the possible matches.
int[] count = new int[indexes.length()];

//Loop through the sentence looking for matches.
for (int i = 0; i < sentence.length(); i++) {
    //This will get the index in the array, if it's a character we are tracking
    int index = indexes.indexOf(sentence.charAt(i));

    //If it's not a character we are tracking, indexOf returns -1, so skip those.
    if (index < 0)
        continue;

    count[index]++;
}

Then you can print them all out with this:

for (int i = 0; i < count.length; i++) {
    if (count[i] < 1)
        continue;

    System.out.println(String.format("%s (%d) %s",
            indexes.charAt(i),
            count[i],
            //This little bit of magic creates a string of nul bytes, then replaces it with asterisks.
            new String(new char[count[i]]).replace('\0', '*')));
}

If you aren't comfortable with the new String(new char[count[i]]).replace('\0', '*')) bit, then you can use a StringBuilder to build the asterisk String before trying to output it. You can see @mike's example below for a good example of that.

Outputs

1 (1) *
2 (1) *
3 (1) *
A (3) ***
B (3) ***
C (3) ***

Considerations

Here are some things to consider when decided how to solve this problem.

  • Will you always know what characters need to be tracked ahead of time, or will there be times when you want to track any character? In the latter case, an array won't work for you; you would need to use an advanced data structure like a TreeMap or HashMap.
  • Will you always be counting occurrences of specific chars, as opposed to Strings? If you have to modify this to count Strings then using the String indexes map trick isn't going to work for you either.
  • Are you learning a specific data structure(s) in your course at the moment? Usually problems such as this are assigned to students to understand how to apply a specific concept. As @kyle suggested, you should try to use the data structure that you are learning about, or have learned about, in your class. Sometimes using structures that you haven't learned about yet can get you in trouble, or a lower grade at least.
like image 54
crush Avatar answered Oct 03 '22 12:10

crush