Search code examples
ccs50undefined-behaviorc-stringsnull-pointer

Segmentation Fault While Manipulating String


I'm trying to create a Ceasar Cipher. For this test, I used a key of 1 and plain text of "hello." When doing so, I get the error message "Segmentation fault (core dumped)". I know this means I am trying to access an illegal memory location, and it's happening while calling the "encrypt" function, but that's all I know.

Depiction of the error I'm getting while debugging.

And here is my code.

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

bool isValidKey(string);
string encrypt(string, int);

string c;

int main(int argc, string argv[])
{
    if (argc != 2 || isValidKey(argv[1]) == 0)
    {
        printf("Useage: ./caesar key\n");
        return 0;
    }
    string p = get_string("plaintext: ");
    c = encrypt(p, atoi(argv[1]));
    printf("%s", c);
    return 0;
}

bool isValidKey(string key)
{
    for (int i = 0; i < strlen(key); i++)
    {
        if (isdigit(key[i]) == 0)
        {
            return false;
        }
    }
    return true;
}

string encrypt(string plain, int k)
{
    for (int i = 0; i < strlen(plain); i++)
    {
        if (isalpha(plain[i]) != 0)
        {
            if (islower(plain[i]) != 0)
            {
                c[i] = ((plain[i] - 97 + k) % 26) + 97;
            }
            else
            {
                c[i] = ((plain[i] - 65 + k) % 26) + 65;
            }
        }
    }
    return c;
}

Solution

  • You need to allocate memory for c.

    string encrypt(string plain, int k)
    {
        c = malloc(strlen(plain) + 1);
    
        for (int i = 0; i < strlen(plain); i++)
        {
            if (isalpha(plain[i]) != 0)
            {
                if (islower(plain[i]) != 0)
                {
                    c[i] = ((plain[i] - 97 + k) % 26) + 97;
                }
                else
                {
                    c[i] = ((plain[i] - 65 + k) % 26) + 65;
                }
            }
        }
        return c;
    }
    

    And in main() you should add free(c); before return 0;