String compression

test case 3 is not passing

#include
using namespace std;
#include
int main() {
string s1;
getline(cin,s1);
string ans="";
int i=0;
int j=0;

while(i<s1.length()){
	while(j<s1.length() && s1[i]==s1[j]){
		j++;
	}
    int count=j-i;
	ans=ans+s1[i]+char(count+'0');
	i=j;
}
s1=ans;
cout<<s1<<endl;
return 0;

}

Hello @rabimajumder08,

ans=ans+s1[i]+char(count+β€˜0’);

This line is wrong if the count is more than 9 let’s say 12 then it will show the wrong output.

Correct way,

ans=ans+s1[i]+to_string(count);

to_string converts numbers into string.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.