I am trying to change the chars of a string one by one with for loop and the segmentation fault occurs in real_A[i]=ciphertext[i];
, that's the code to change the chars of the real_A string with the ciphertext.
here is my code:
string substitution(string plaintext, string ciphertext)
{
string real_A="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string real_a="abcdefghijklmnopqrstuvwxyz";
//change alphabets to ciphertext
for (int i=0,n=strlen(ciphertext);i<n;i++)
{
if (isupper(ciphertext[i]))
{
real_A[i]=ciphertext[i];
real_a[i]=real_A[i+32];
}
else if (islower(ciphertext[i]))
{
real_a[i]=ciphertext[i];
real_A[i]=ciphertext[i-32];
}
for (int i=0,n=strlen(plaintext);i<n;i++)
{
if (isupper(plaintext[i]))
{
//get the ascii num
int letter=plaintext[i]-65;
plaintext[i]=real_A[letter];
}
else if (islower(ciphertext[i]))
{
int letter=plaintext[i]-97;
plaintext[i]=real_a[letter];
}
}
return plaintext;
}
How do I deal with this bug? I tried to change the string real_A="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
to string real_A[26];
but it got me more errors.