String compression

How to solve this problem .I am able to print the output but i can’t find any logic to compress .
How to convert int to character

hey @Alfred
You can use to_string() function
it converts given integer to string type and then u can extract chars from it to add to char array/string

1 Like

I send you the link of the code.plzz tell me my mistake

Hey @Alfred

			string num=to_string(b); //converts b to string 
            for(int r=0;r<num.length();r++){ //traverse over string
                temp[j++]=num[r]; //add all digits as chars to array
            } 

This is the logic for adding num
try to do the rest on ur own

#include<iostream>
#include<cstring>

using namespace std;
int main() {
	char a[1000];
	cin>>a;
	char temp[1000];
	temp[0]=a[0];
	int j=0;
	int k=0;
	for(int i=1;i<=strlen(a);i++){
		if(temp[j]!=a[i]){
			j++;
			int b=i-k;
			string num=to_string(b);
			for(int r=0;r<num.length();r++){
				temp[j]=num[r];
				j++;//added here
				}
			// j++;removed this
			temp[j]=a[i];
			k=i;
       }
	}
	for(int i=0;i<strlen(temp);i++){
		a[i]=temp[i];
	}
	a[strlen(temp)]='\0';
	for(int i=0;i<strlen(a);i++){
		cout<<a[i];
	}
	return 0;
}

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.