1

I've reached the final step of Vigenere, but I don't really understand how do I loop the key over plaintext. This is the shift function I've created:

int shift(char c)
{
    if(isalpha(c))
    {
        if(isupper(c))
        {
            int b = c - 65;
            return b;  
        }
        if(islower(c))
        {
            int b = c - 97;
            return b;
        }
    }
    {
        printf("\n");
        return 0;
    }
}

While trying to understand how I do that, I've looked up examples and it seems that you need for loop with 2 (1 for keyword and other for plaintext) counters, but hints of this problem states that "it's easier to control the keyword counter yourself, rather than relying on the for loop ", so this makes me a bit confused.
Let's say I use for loop with 2 counters: how does these counters affect my plaintext, considering that I'm only changing the shift function? And how does for loop with 2 counters work exactly?
I hope this mess makes sense.

1 Answer 1

1

From Final Steps in the spec:

Remember also that every time you encipher a character, you need to move to the next letter of k, the keyword (and wrap around to the beginning of the keyword if you exhaust all of its characters). But if you don’t encipher a character (e.g., a space or a punctuation mark), don’t advance to the next character of k!

Perhaps it is the concept of "a loop with two counters" that is causing confusion.

You know how to "loop" over every letter of the plaintext, something like:

for (int i = 0; i < strlen(plaintext); i++) {
    // encipher plaintext[i] 
}

The shift function is written to get the "shift" value of the key. In the simple example (a one letter key), it is called like this int key = shift(argv[1][0]);. In that sentence [0] means the first character. But what if the keyword is "dog"? Then there are three letters. How would you move to the next letter of the keyword? You would need to call the shift function with argv[1][1], then argv[1][2], then "loop back" to the start (argv[1][0]).

The "second counter" is a variable that keeps track of which character to use in the keyword. Since where you are in the plaintext (i for instance) is not connected to where you are in the keyword (j for instance), you will likely want to use the loop as above to traverse all the characters in the plaintext, and then "control the keyword counter" with no concern for the plaintext counter. The only thing j depends on is the length of the keyword.

You must log in to answer this question.

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