public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
String str = scn.nextLine();
char s = str.charAt(0);
int count = 1;
for (int i = 1; i < str.length(); i++) {
char curr = str.charAt(i);
char prev = str.charAt(i - 1);
if (curr == prev)
{
count++;
}else {
if (count > 1) {
s+=count;
count = 1;
}
s+=curr;
}
}
if(count>1) {
s+=count;
count = 1;
}
System.out.print(s);
}
}
Code not working
this increments the character ,instead of appending
use string builder along with append
here is your corrected code:
if this solves your doubt please mark it resolved 