hi
i need help with exercise, i have a string: "aabbbacddd".
i need to run on that string and return evey set of letters like this:
'a' 2; 'b' 3; 'a' 1; 'c' 1...;
the trick part is when a letter returns again in the string after different letter.
in the first code it returns value 1,1,1 i need 3.
please help.
i tried writing all sort of stuff but didnwt get the answer.
public int countLetters(char letter, String s) { int length = s.length()-1; //using the string object method length int value = 0; //initialize value integer to store the list value for(int i = 0; i <= length; i++) //moving along all the characters in the string { value = 1; //first, each char is get it own value(1) if(i < length && s.charAt(i)==s.charAt(i+1)) //if the char equals the next char - { value++; //add +1 to value i++; //move to the next char } } return value; }
public static int countLetter(char c, String s) { int value = 0; int length = s.length()-1; for(int i = 0; i <= length; i++) { if(s.charAt(i) == c) { value++; } } return value; }
10x