0

I'm at my wits end with this pset2 exercise. When I test my code I get a single error:

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQGoh\n"

Why in the name of all that is holy would my code work for everything else but this one combination? Fuh realz.

Here's what I got:

int y = 0;

// encipher text
for (int i = 0; i < strlen(phrase); i++) {

    // exclude non-alpha && blank space values
    if ((isalpha(phrase[i])) && (phrase[i] != ' ')) {

        // encode uppercase letters
        if (isupper(phrase[i])) {
            int upper = (((phrase[i] - 'A') + (keyword[y] - 'A')) % 26) + 'A';
            printf("%c", upper);
        } 

        // encode lowercase letters
        else if (islower(phrase[i])) {
            int lower = (((phrase[i] - 'a') + (keyword[y] - 'a')) % 26) + 'a';
            printf("%c", lower);
        }

        // increment or reset keyword character index based on length of keyword - 1
        if (y < (strlen(keyword) - 1)) { y++; } else { y = 0; }
    }

    // otherwise, copy existing character, no adjustment
    else {
        printf("%c", phrase[i]);
    }
}

What gives? Learn me! I am an empty vessel, ready to be schooled in the ways of debuggery!

2 Answers 2

1

I'd need to see more of your code or spend more time to understand what's going on, but a couple of points:

\n is the return character, are you creating a newline anywhere in your code?

Secondly, check this question: Vigenere fails check50 testencrypts "BaRFoo" as "CaQGon" using "BaZ" error

1
  • 1
    Thanks for the pointer! (Get it? Pointer? Coder joke! : D )
    – Kyle
    Commented Nov 28, 2016 at 19:29
1

As it turns out, not only do you want to check pre-encoded string characters for case, you also have to check the encoding key characters for case to ensure that the values line up appropriately. My updated code is:

int y = 0;

// encipher text
for (int i = 0; i < strlen(phrase); i++) {

    // exclude non-alpha && blank space values
    if ((isalpha(phrase[i])) && (phrase[i] != ' ')) {

        // encode uppercase letters & check case of key value
        if (isupper(phrase[i]) && isupper(keyword[y])) {
            int upper = (((phrase[i] - 'A') + (keyword[y] - 'A')) % 26) + 'A';
            printf("%c", upper);
        }

        // encode uppercase letters & check case of key value
        else if (isupper(phrase[i]) && islower(keyword[y])) {
            int upper = (((phrase[i] - 'A') + (keyword[y] - 'a')) % 26) + 'A';
            printf("%c", upper);
        }

        // encode lowercase letters & check case of key value
        else if (islower(phrase[i]) && islower(keyword[y])) {
            int lower = (((phrase[i] - 'a') + (keyword[y] - 'a')) % 26) + 'a';
            printf("%c", lower);
        }

        // encode lowercase letters & check case of key value
        else if (islower(phrase[i]) && isupper(keyword[y])) {
            int lower = (((phrase[i] - 'a') + (keyword[y] - 'A')) % 26) + 'a';
            printf("%c", lower);
        }

        // increment or reset keyword character index based on length of keyword - 1
        if (y < (strlen(keyword) - 1)) { y++; } else { y = 0; }
    }

    // otherwise, copy existing character, no adjustment
    else {
        printf("%c", phrase[i]);
    }
}
2
  • 1
    If you want a better way to deal with this, you could just always convert keyword to lowercase before you start using it in the rest of the code. If you do that, you can use only (keyword[y] - 'a') and ditch the keyword[y] - 'A' conditions. Commented Nov 28, 2016 at 19:40
  • 1
    Of course! I see it now. ( . ) ( . ) Thank you! : D
    – Kyle
    Commented Nov 28, 2016 at 22:11

You must log in to answer this question.

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