#include
#include<bits/stdc++.h>
using namespace std;
int main()
{ char s[1000],ans[2000];
int l,d,j=0;
cin.getline(s,1000);
l=strlen(s);
for(int i=0;i<l-1;i++)
{ d=s[i+1]-s[i];
if(d<0)
{ ans[j]=s[i];
j=j+1;
ans[j]=’-’;
j=j+1;
ans[j]=0-d+‘0’;
j=j+1;
}
else
{ ans[j]=s[i];
j=j+1;
ans[j]=d+‘0’;
j=j+1;
}
}
d=strlen(ans);
ans[d]=s[l-1];
cout<<ans;
return 0;
}
Why am i getting a wrong answer in 1st testcase
Hello @bhavyajain,
From next time, share your code using Coding Block Online IDE, the way you have shared is introducing many syntax error in the code.
Steps:
- Save your code on IDE
- Share the URL of your code.
Now, coming back to your code:
Your code is not working for the testcases like:
aza
Expected Output:
a25z-25a
What if the input string is of length is 1000 and the difference is negative and two digit number,
then the size of the array ans[2000] is not appropriate.
I would recommend you to rather use string as
2.1. you need not specify the size
2.2. it is easier to concatenate strings.
If you still face issues, let me know.
Hope, this would help.
Give a like, if you are satisfied.