How to count the characters only if they are right next to each other, otherwise just print the characters as they are. I wrote the following code based on my little knowledge of Java.
for example String w="kjiikkkjial";
the result should be: kji2k3jial
String am ="asbbaamkkkjkssg";
char op = 0;
char[] ch = am.toCharArray();
for(int i=0; i<ch.length; i++) {
int k=1;
for(int j=i+1; j<ch.length; j++) {
if(ch[i]==ch[j]) {
k++;
op=ch[j];
i=j;
}
else break;
}
if(k>1)
System.out.print(op+""+k);
else
System.out.print(ch[i]);
}