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

bool isNumber(char number[]);

int main(int argc, string argv[])
{
    // command line argument validation
    if (argc <= 0 || argc > 2 || isNumber(argv[1]))
    {
        printf("error\n");
        return 1; //error output
    }

    int i;
    int j = 0;
    char ascii;
    string k = argv[1];

    //ensure if the keyword is alphabetical, if not bye bye!
    for (i = 0; i < strlen(k); i++)
    {
        if (!isalpha(k[i]))
        {
            return 1;
        }
    }


    printf("plaintext: ");
    string plaintext = get_string();

    printf("ciphertext: ");

    // iterating over the plaintext
    for (i = 0; i < strlen(plaintext); i++)
    {
        if (isalpha(plaintext[i]))
        {
            if (isupper(plaintext[i]))
            {
                ascii = ((plaintext[i] - 65) + (toupper(k[j]) - 65)) % 26;
                printf("%c", ascii + 65);
            }
            else if (islower(plaintext[i]))
            {
                ascii = ((plaintext[i] - 97) + (tolower(k[j]) - 97)) % 26;
                printf("%c", ascii + 97);
            }
            j = (j + 1) % strlen(k);
        }
        else{
            printf("%c", plaintext[i]);

            if (j >= strlen(k))
            {
                j = 0;
            }
        }


    }

    printf("\n");

    return 0;
}

bool isNumber(char number[])
{
    int i = 0;

    //checking for negative numbers
    if (number[0] == '-')
        i = 1;
    for (; number[i] != 0; i++)
    {
        //if (number[i] > '9' || number[i] < '0')
        if (!isdigit(number[i]))
            return false;
    }
    return true;
}

This is my code, it's working but the error presented in check50 is:

:( handles lack of argv[1] failed to execute program due to segmentation fault

I don't get it, please some advice.

0

You accept argc of one or two, but only two (meaning one argument) is valid. Why not go with the != not equal operator? It's shorter than a argc < 2 || argc > 2.

|| isNumber(argv[1]) made sense for Caesar, but not Vigenère.

1
  • It worked...I didn't look at this part, thanks – Rafael Áquila Dec 5 '17 at 14:33

You must log in to answer this question.

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