Why is 3rd test case coming wrong in string compression?
your code is producing wrong output.
there is a problem with the logic you have applied.
please, first try to resolve it yourself.
make a dry run on these inputs:
- abbaabaaa
expected output: a1b2a2b1a3 - aaabbbab
expected output: a3b3a1b1
#include
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{ int p,c=0;
char a[100];
cin>>a;
int l=strlen(a);
for(int i=0;i<l;i=i+p)
{
for(int j=i;j<l;j++)
{
if(a[i]==a[j])
{
c++;
}
else if((a[i]!=a[j]))
{
break;
}
}
p=c;
c=0;
cout<<a[i]<<p;
}
return 0;
}
i have corrected your code.
figure out the modifications and the reason for that modition.
think for why? why are we doing this?
solution:
dry run your program to understand that.
if you still face problem, feel free to ask.