Home > database >  "Segmentation fault" in C code - My code works on an encryption problem
"Segmentation fault" in C code - My code works on an encryption problem

Time:01-23

I am working on a code to do encryption based on a given key. When I execute the code, it runs smoothly to check the command line input. However, after I input the plaintext, the code results in "Segmentation fault". I looked over my code but don't know what is wrong. I really appreciate any help.

This is my code:

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
    


int encryption(string plaintext, int count_pt, int key);

int main(int argc, string argv[])
{
    // Check if the program takes two arguments
    if (argc != 2)
    {
        printf("%s", "The program takes two arguments.");
        return 1; 
    }
    
    // Count the characters of the second argument
    int count = strlen(argv[1]); 
    
    // Make sure the second argument is digit only
    for (int i = 0; i < count; i  )
    {
        if (isdigit(argv[1][i]))
        {
            printf("%s\n", "Success.");
            
            // Convert the string into integer
            int key = atoi(argv[1]);
            
            // Prompt the user for plaintext
            string plaintext = get_string("Please provide the plaintext: ");
            int count_pt = strlen(plaintext);
            int result = encryption(plaintext, count_pt, key);
            printf("%d", result);
        }
        
        else if (!isdigit(argv[1][i]))
        {
            printf("%s\n", "Fail.");
            return 1;
        }
    }
}

int encryption(string plaintext, int count_pt, int key)
{
    int char_array[count_pt];
    int ascii_shift = 0;
    //Encrypt the plaintext by wrapping the characters based on the key
    for (int i = 0; i < count_pt; i  )
    {
    //Check if characters are alphabetic
        if (isalpha(plaintext))
        {
               if (isupper(plaintext)) 
               {
                   ascii_shift = plaintext[i] - 'A';
                   char_array[i] = ((ascii_shift   key)  % 26)   'A';
               }
               
               if (islower(plaintext))
               {
                   ascii_shift = plaintext[i] - 'a';
                   char_array[i] = ((ascii_shift   key)  % 26)   'a';
               }
        }
        else 
        {
             //Keep the non-alphabetical characters the same 
             char_array[i] = plaintext[i];
        }
    }
    
    return char_array[count_pt];
}

CodePudding user response:

The problem is in the function encryption. Most specifically in this part...

if (isalpha(plaintext))
{
    if (isupper(plaintext)) 
    {
        ascii_shift = plaintext[i] - 'A';
        char_array[i] = ((ascii_shift   key)  % 26)   'A';
    }
           
    if (islower(plaintext))
    {
        ascii_shift = plaintext[i] - 'a';
        char_array[i] = ((ascii_shift   key)  % 26)   'a';
    }
}

The functions isalpha(), isupper() and islower() take int arguments but you give them plaintex which is a pointer (hexadecimal number representing memory address). To fix the issue you need to pass the character of the plaintext. You can do it in two ways.

  1. Accessing characters by index
if (isalpha(plaintext[i]))
{
    if (isupper(plaintext[i])) 
    {
        ascii_shift = plaintext[i] - 'A';
        char_array[i] = ((ascii_shift   key)  % 26)   'A';
    }
           
    if (islower(plaintext[i]))
    {
        ascii_shift = plaintext[i] - 'a';
        char_array[i] = ((ascii_shift   key)  % 26)   'a';
    }
}
  1. With the help of dereferencing
if (isalpha(*plaintext))
{
    if (isupper(*plaintext)) 
    {
        ascii_shift = plaintext[i] - 'A';
        char_array[i] = ((ascii_shift   key)  % 26)   'A';
    }
           
    if (islower(*plaintext))
    {
        ascii_shift = plaintext[i] - 'a';
        char_array[i] = ((ascii_shift   key)  % 26)   'a';
    }
}
  •  Tags:  
  • Related