We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I am trying to figure out how to do the following:
Essentially I am trying to figure out a way of counting the frequency of each different character in a string.
I was able to do this using if/else statements for each letter of the alphabet but ideally I would like a better way of doing this.
Heres what I have so far:
String yourString = msg;
char[] allChars = new char[yourString.length()];
for (int i = 0; i < yourString.length (); i++) {
allChars[i] = yourString.charAt(i);
String character = str(allChars[i]);
for (counter=0; counter < alpha.length; counter++) {
if (character.equals(alpha[counter]) == true ) {
count[counter] = count[counter]+1;
}
}
println("The number of A is" + count[0]);
}
alpha is a character array containing the individual letters of the alphabet count is an array where I am trying to store the values i.e:
Any help would be great! thanks!
Answers