1 / 4
Apr 2018

site : http://www.spoj.com/problems/GNY07A/5
Language : C++
my solution : https://ideone.com/ExrR7X4
#include <iostream
#include <string
using namespace std;

int main()
{
long int T,t,n,a,x,b;
string str1,str2;
cin >> T;
for (t=1; t<=T; t++)
{
cin >> n;
n–;
cin >> str1;
a = str1.length();
b = 0;
for (x=0; x<a; x++)
{
if (x != n)
{
str2[b] = str1[x];
b++;
}
}
cout << t << " ";
for (b=0; b < a-1; b++)
{
cout << str2[b];
}
cout << “\n”;
}
}
Note: I put on the 2 last > in #include iostream and string but didn’t put it there because somehow it disappears when I put it here.

I got runtime error (SIGSEGV). Can anyone tell me why does it give an error and how to fix it? It went ok in Ideone. Thx

  • created

    Apr '18
  • last reply

    Apr '18
  • 3

    replies

  • 804

    views

  • 2

    users

  • 2

    links

You haven’t allocated any memory for str2, so who knows what you might be overwriting when you copy str1 over it.

Well, ok let me try. But why did Ideone still runs it perfectly? It also gives the correct output based on the problem

Because C++ is good like that. Incorrect code can appear to run perfectly, right up to the moment when it doesn’t.

btw, did you try an 80 character string? That’s when I got a runtime error.

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 17 10d

Want to read more? Browse other topics in C and C++ or view latest topics.