(This is not homework)
We have some extra exercices we can do, and i have done some.
But i got stuck in this one…
I need to make a program that given the string “loool” prints “l:1:o:3:l:1”.
I have tried a bunch of combinations but i keep getting the same problem:
– I cant make the last repeated letter to get print ( Because with my code the next char needs to be different for a print to occurr).
String str = "loool";
StringBuilder sb = new StringBuilder();
int count = 1;
char before;
before = str.charAt(0);
for (int i = 1;i < str.length();i++) {
if (str.charAt(i) == before) {
count++;
}
else {
sb.append(before + ":" + count);
before = str.charAt(i);
count = 1;
}
}
return sb.toString();
You need to add some logic after your loop has finished, in order to deal with this problem. This logic will probably be very similar to the some of the code that you’re using in the
elseblock.