I'm getting a segmentation error as soon as I execute this program. I've run it through the debugger and it works fine and outputs the correct ciphertext. There's gotta be something going on in the first few lines but I can't figure out what.

If it helps, when I execute, in the debugger, it lists (what I imagine is) an unusual value for argv: enter image description here

Code is below. Can anyone point me in the right direction?

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

int main(int argc, string argv[])
    if (argc ==2 && isdigit(argv[1]))             //if argc = 2 AND argv[1] is digits
        int k = atoi(argv[1]);                      //initialize k as the conversion of argv[1] to an integer

        string plaintext = get_string("Plaintext: ");   //get user input for plaintext
        printf("Ciphertext: ");

        for (int i = 0, len = strlen(plaintext); i < len; i++)    //i is the current character its looking at
            if (plaintext[i] >= 'a' && plaintext[i] <= 'z')     //if a or higher, or z or lower
                printf("%c", (((plaintext[i] - 'a') + k) % 26) + 'a');  //then print letter + key
            else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')     //if A or higher, or Z or lower
                printf("%c", (((plaintext[i] - 'A') + k) % 26) + 'A');  //then print letter + key
                printf("%c", plaintext[i]);                     //otherwise print whatever character is there

        return 0;


You're right, it's in the first few lines.

if (argc ==2 && isdigit(argv[1]))

The function isdigit() and all of it's cousins take just one char as input. The code is trying to feed it an entire string, so it chokes and coughs up a seg fault.

As for debug50 running ok, well, that's a well known "feature" of debug50 and the underlying debugger. Sometimes, they just won't throw a seg fault when they should. They can mask an error quite effectively, as frustrating and as wrong as that is! In that case, you need to fall back on inserting printf statements.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .