Take as input S, a string. Write a function that does basic string compression. Print the value returned. E.g. for input “aaabbccds” print out a3b2c2d1s1

#include
#include
#include
using namespace std;

void String_Comp(char s[]){
//initilaising array for frequency count
int freq[26]={0};
char ch;
int len=strlen(s);
int max=0;
for(int i=0;i<len;i++){
freq[s[i]-‘a’]++;
}
for(int i=0;i<26;i++)
{
if(freq[i]!=0){
ch=i+‘a’;
cout<<ch<<freq[i];
}
}

}

int main() {
char s[1000];
cin.getline(s,1000);
String_Comp(s);

return 0;

}

Here I’m getting two test cases wrong.Can you please point out where am I going wrong?

Hey @mehul.ai
Please re-read the question u have to compress consecutive chars
For
aabbaaa
your output should be
a2b2a3

Also the count of number can be >9

1 Like

Hey @mehul.ai
image

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.