Why it is giving sigmentation fault for this snippet

when I am submitting this code it giving SIGSEGV fault
constrains are:
1 ≤ N ≤ 16
1 ≤ N ≤ 10^3
1 ≤ N ≤ 10^5
for the first condition, it is working
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
string a,b;
string z;
cin>>a>>b;
int n=a.length();
for(int i=0;i<n;i++)
{
if(a[i]==‘W’ && b[i]==‘W’)
z[i]=‘B’
else if(a[i]==‘W’ && b[i]==‘B’)
z[i]=‘W’;
else
z[i]=‘B’;
}
cout<<z<<endl;
}
}
but when I am performing arithmetic operation for append character in string it is working fine why it is happening
program:
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
string a,b;
string z;
cin>>a>>b;
int n=a.length();
for(int i=0;i<n;i++)
{
if(a[i]==‘W’ && b[i]==‘W’)
z+=‘B’;
else if(a[i]==‘W’ && b[i]==‘B’)
z+=‘W’;
else
z+=‘B’;
}
cout<<z<<endl;
}
}