Getting Segmentation fault error

#include <bits/stdc++.h>
#include <string>
//#include <stdlib.h>
using namespace std;

int main()
{
   long int t,i,j,len,flag=0;
   string str1,str2;

   cin>>t;

while(t>0){

cin>>str1;

len=str1.length();

//cout<<len<<'\n';

for (int i = 0; i < len; i++) {
            str2[i] = str1[str1.length() - i - 1];
        }

for(i=1;i<len;i++){

if(abs(str1[i] - str1[i-1]) != abs(str2[i] - str2[i-1])){

flag=1;
break;
}
}

if(flag==0){

    cout<<"Funny\n";

}else{
    cout<<"Not Funny\n";
}
/*for(i=0;i<len;i++){

    cout<<str2[i];

}*/

t--;

}

    return 0;
}

What Problem are you trying to solve/ what inputs are triggering the error?

2 Likes

Ok, I’m guessing it’s Funny String.

One thing that will cause a crash is that str2 is empty, so any attempt to index an element (e.g. str2[i] ) will be Undefined Behaviour (most likely a crash).

With that fixed, I seem to get the correct answer for the sample testcase, at least.

2 Likes

yes it is funny string…ok i will try something else

1 Like

It’s working perfectly @manoj_rawat .
No need to make any changes

try this string: ovyvzvptyvpvpxyztlrztsrztztqvrxtxuxq