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:
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:
aCounter
through zCounter
plus 0counter
through 9counter
I'm not asking for an set answer here, I'm just looking for some good directions, because I'm stuck.
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.
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.
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.
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.
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.
1 (1) *
2 (1) *
3 (1) *
A (3) ***
B (3) ***
C (3) ***
Here are some things to consider when decided how to solve this problem.
char
s, as opposed to String
s? If you have to modify this to count String
s then using the String indexes
map trick isn't going to work for you either.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With