I'm trying to build a function, that gets a string of letters, and prints the amount of each letter in the string. for example: input: String = "aaabbaccxyxyx" output: 4a2b2c3x2y
This is what I've come up with:
public class Q1 {
public static String numLetters(String s){
String end = new String();
int counter = 0;
char c,d;
for(int i=0; i<s.length();i++){
c = s.charAt(i);
for(int j=0; j<s.length();j++){
d = s.charAt(j);
if(c == d){
counter++;
}
}
end = end + counter+c;
counter = 0;
}
return end;
}
but, this is the output: 4a4a4a2b2b4a2c2c3x2y3x2y3x A lot of repeats..
Any help how to make it right? Keep in mind, the function needs to return a string, not just prints it out. Thanks! =)