0

I have finally finished writing my code for the Substitution question in Problem Set 2, but Check50 tells me that my code has a segmentation fault when there is a lack of key. My code is as follows:

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

int main(int argc, string argv[])
{
    int n = strlen(argv[1]);
    if (argc == 2 && n == 26)
    {
        for (int i = 0; i < n; i++)
        {
            if (!(isalpha(argv[1][i])))
            {
                printf("Please enter a valid substitution key in the command-line. Your key must contain each and every letter of the alphabet only once in no particular order, but nothing more.\n");
                return 1;
            }
            else
            {
                for (int j = 0; j < n; j++)
                {
                    if (i != j && argv[1][i] == argv[1][j])
                    {
                            printf("Please enter a valid substitution key in the command-line. Your key must contain each and every letter of the alphabet only once in no particular order, but nothing more.\n");
                            return 1;
                    }
                }
            }
        }
    }
    else
    {
        printf("Please enter a valid substitution key in the command-line. Your key must contain each and every letter of the alphabet only once in no particular order, but nothing more.\n");
        return 1;
    }
    string ptext = get_string("plaintext: ");
    printf("ciphertext: ");
    for (int k = 0, o = strlen(ptext); k < o; k++)
    {
        if (islower(ptext[k]))
        {
            printf("%c", tolower(argv[1][ptext[k] - 'a']));
        }
        else if (isupper(ptext[k]))
        {
            printf("%c", toupper(argv[1][ptext[k] - 'A']));
        }
        else
        {
            printf("%c", ptext[k]);
        }
    }
    printf("\n");
    return 0;
}

Indeed, when I try to execute my program without a key, it does result in a segmentation fault, as shown here:

~/pset1/substitution/ $ make substitution
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    substitution.c  -lcrypt -lcs50 -lm -o substitution
~/pset1/substitution/ $ ./substitution
Segmentation fault

I thought that

else
    {
        printf("Please enter a valid substitution key in the command-line. Your key must contain each and every letter of the alphabet only once in no particular order, but nothing more.\n");
        return 1;
    }

would have already considered the case when arcc == 1, that is, when there is no key? Any intuitions and reasons as to why my code has a segmentation fault and where that fault might lie would be greatly appreciated :)

4

Think about what this code is doing;

int main(int argc, string argv[])
{
    int n = strlen(argv[1]);

If there's no argument, then argv[1] doesn't exist. Trying to access it generates a seg fault. That's why it's necessary to check the value of argc before trying to use any argv element.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

You must log in to answer this question.

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