blowin_in_the_wind's blog

By blowin_in_the_wind, history, 8 years ago, In English

getting runtime error on testcase 11

Exit code is -1073741819

here is my code:

include include<string.h> include

using namespace std; int main() { string lame=""; string a,b="",c=""; int i,j,n; getline(cin,a);

for(i=0;i<a.length();i++)
 b[i]=a[i];
 sort(a.begin(),a.end());j=0;

for(i=0;i<a.length();i++)
{
    if(b[i]!=a[j])
    {
           lame.push_back(b[i]);

    }
    else
    {
       c.push_back(a[j]);
       j++;
    }

}
if(lame.length()!=0)
{
    reverse(lame.begin(),lame.end());
c=c+lame;
}
cout<<c<<endl;
return 0;

}

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by blowin_in_the_wind (previous revision, new revision, compare).

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it
for(i=0;i<a.length();i++)
  b[i]=a[i];

You're going out of the bounds for b here. At that moment in time b.length() is 0 so you can't assign b[0], b[1] ... b[a.size()-1] yet. Either use b += a[i] instead of b[i] = a[i], or even better, do without the loop completely and just do b = a