Testcases are failing

https://ide.codingblocks.com/s/48969
2 testcase are failing…don’t know why

Please share the corresponding link of the question.

https://hack.codingblocks.com/contests/c/548/100

Hi Gaurav,

  1. Your input does not match the expected input has you have not considered the length of the string as an input.
  2. You have not applied the correct logic for finding CB numbers.
    In case your doubt still persists, please add comments in the code with your logic so that we can debug it using your logic.

I am sorry I think I have sent you wrong problem.
code that I had sent you earlier is for this problem
https://hack.codingblocks.com/contests/c/474/1371

For the sample input aaabbccds, the output should be a3b2c2ds. But your code is giving a3b2c2d1s1. Your code should not print the count when it is 1.

https://ide.codingblocks.com/s/49025
i have updated this code…it is giving correct output for aaabbccds but all testcases are failing here.

Hey Gaurav, your code will not work if given string id of length 1.
for eg.
input:
a

your code’s output:
aÆ0z0

expected output:
a

than what changes I can made to make it work??
#include
#include<string.h>
using namespace std;
int main()
{
char str[100];
char record[100];
int count[100]={0},cnt=0,i,j,l;
cin.getline(str,100);
l=strlen(str);
j=0;
for(i=0;str[i]!=’\0’;i++)
{
record[j]=str[i];
cnt=1;
while(i+1<l&&str[i+1]==str[i])
{
++i;
++cnt;
}
count[j]=cnt;
j++;
}
for(i=0;i<strlen(record);i++)
{
if(count[i]==1)
{
cout<<record[i];
}
else
{
cout<<record[i]<<count[i];
}
}

return 0;
}

tell me problem with this code…I think it is write but testcases are failing due to wrong answer

Hey Gaurav, just do these small modifications to your code,

  1. initialize variable str of type string like this string str;
  2. input str using cin like thiscin>>str;
  3. use length function to calculate length of str l = str.length();
  4. Before for(i=0;i<strlen(record);i++) this for loop assign record[j]='\0';

I have just did these modifications in your code you can refer this.

this is still giving wrong answer…I think question need the count of 1 value too…I made a change…now it is working
#include
#include
#include<string.h>
using namespace std;
int main()
{
//char str[100];
char record[100];
int count[100]={0},cnt=0,i,j,l;
string str;
getline(cin,str);
//cin.getline(str,100);
//l=strlen(str);
l = str.length();
j=0;
for(i=0;str[i]!=’\0’;i++)
{
record[j]=str[i];
cnt=1;
while(i+1<l&&str[i+1]==str[i])
{
++i;
++cnt;
}
count[j]=cnt;
j++;
}
record[j]=’\0’;
for(i=0;i<strlen(record);i++)
{
cout<<record[i]<<count[i];
}
return 0;
}

Hey, can you copy your code on ide, save it and share that link here. Actually the code you copy here becomes very ambiguous and first we have to remove its compilation errors and then debug your code, So it will be better if you can share the ide link.

And the code i have shared with you earlier after making the corrections in your code is working fine for all the cases.