0

I've checked my work, and I am failing the "Hax0r2" keyword test, but I cannot figure out why. My code is supposed to reject any argv[1] that is not all alphabetic characters. Will someone take a look and let me know what I am doing wrong?

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

int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("Usage: ./vigenere k\n");
    return 1;
}
string key = argv[1];
int keylength = strlen(key);
for (int i = 0; i < keylength; i++)
{
    if (!isalpha(key[i]))
    {
        printf("Usage: ./vigenere k\n");
        return 1;
    }
    string plaintext = get_string("plaintext: ");
    int ptlength = strlen(plaintext);
    printf("ciphertext: ");
    for (int j = 0, k = 0; j < ptlength; j++)
    {
        char keyconversion = tolower(key[k % keylength]) - 97;
        char uppercipher = (((plaintext[j] + keyconversion - 65) % 26) + 65);
        char lowercipher = (((plaintext[j] + keyconversion - 97) % 26) + 97);
        if isupper(plaintext[j])
        {
            printf("%c", uppercipher);
            k++;
        }
        else if islower(plaintext[j])
        {
            printf("%c", lowercipher);
            k++;
        }
        else
        {
            printf("%c", plaintext[j]);
        }
    }
    printf("\n");
    return 0;
}

}

2
  • The next line would be just }, right? Is that the version you are submitting (after having saved), or are you accidentally submitting a different file?
    – Blauelf
    Commented Oct 11, 2018 at 8:46
  • Hi. That wasn't the whole code, but I just added it in case it helps. Everything else works except for the part I mentioned. Commented Oct 11, 2018 at 12:22

1 Answer 1

1

When checking for non-alphabetic characters, your loop should do exactly that, nothing more. After that loop, when you know no non-alphabetic character was in the keyword (as you would have returned otherwise), ask for plain text.

So it's basically moving one } from the end up to after what you showed initially.

1
  • Wow, thanks for making me take a harder look! Fantastic! Commented Oct 11, 2018 at 12:49

You must log in to answer this question.

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