-2

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.

Nagev
  • 10,835
  • 4
  • 58
  • 69
  • 2
    It seems you are trying to change a string literal. Any attempt to change a string literal results in undefined behavior. – Vlad from Moscow Feb 17 '21 at 11:16
  • 1
    In any case the function does not make a sense at least because it returns nothing though its return type is not void and the parameter plaintext is not used within the function.:) – Vlad from Moscow Feb 17 '21 at 11:19
  • 1
    Please provide a [mre]. At least to better check what the keyword `string` here refers to. – Damien Feb 17 '21 at 11:21
  • @Damien string is a typedef name for char *.:) – Vlad from Moscow Feb 17 '21 at 11:22
  • @ Vlad from Moscow , yeah sorry I didn't include the whole function since the bug appears in the initial lines of the code and i dont want to cause confusion if i add the rest of the code – patrick chen Feb 17 '21 at 11:26
  • `"abcde"[1] = 'q';` will probably segfault, too. – wildplasser Feb 17 '21 at 11:28
  • 1
    A quick fix is to replace `string real_A="...";` with `char real_A[]="...";`, and similarly for `real_a`. That will define `real_A` and `real_a` as arrays of `char` that can be modified. – Ian Abbott Feb 17 '21 at 11:29
  • @Damien I'm new to C and that's the only way I know to assign string, but thanks tho now I know the way. – patrick chen Feb 17 '21 at 11:31
  • 2
    @Damien `typedef char *string` is a reviled and misleading CS50 abomination known for confusing way too many C novices. – Andrew Henle Feb 17 '21 at 11:36
  • @IanAbbott Thankyou so much!! Yes it does answer my question and your solution fix the segmentation fault – patrick chen Feb 17 '21 at 12:03

1 Answers1

0

Since you are using string I'm going to assume you are using C++ not C?!

Your code produces a segmentation fault since you are using an index that is out of range here:

real_a[i]=real_A[i+32];

Since i is in the range 0 up to the length of ciphertext parameter and your real_A string is only of size 26, the index will always be out of range.

You have a similar issue here:

real_A[i]=ciphertext[i-32];

Note: Assuming this is C++ code and the string you are using is std::string, real_a and real_A are strings initialized with literals, they are not string literals. In that case you write to std::string indices just fine if your indices are valid. For example, this is correct C++ code:

std::string a = "Test";
a[0] = 'B';
std::cout << a << std::endl;
Elijan9
  • 1,269
  • 2
  • 17
  • 18
  • Thanks for pointing out that mistake but actually the segmentation fault is because of https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-char-s-initialized-with-a. Sorry it's not C++, it's C. I'm using a library that eases beginners that's why I use `string` to assign string literals. – patrick chen Feb 17 '21 at 12:01