0

I have a problem with the keyword from vigenere. The password does not want to wrap around the text like it should. The first time it works, after that it makes mistakes.. Here is what check50 prints:

:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "xoqmj, yfz gflkp!\n"

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

:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "CAQ3GV\n"

I tried to make a nested for loops. First a for loop to loop through the sentence and the second to loop through the key.

How can I make the keyword/password loop in the right way through the text?

1 Answer 1

1

I'm not sure what you mean by "password" but, to get terms clear, I'll use "plaintext" for the message you input once the program is running and "key" as the key you enter at the command line argument.

1) You can't solve vigenere with nested loops. If you have one loop to iterate the plaintext and another inside of it to iterate the key, you will have a total of (length of plaintext) * (length of key) iterations, and you will print too many letters. Use only one loop, that iterates the plaintext.

2) You need 2 separate indexes: One to index the plaintext, and another one to index the key, so that they can advance independently and the key doesn't change when the plaintext char is not alpha. One loop, two different indexes.

3) If your output is something like "CAQ3GV", take a careful look at how you're actually encrypting: There shouldn't be any numbers regardless of the fact that you're iterating the key wrong. So take a very close look at how you're handling upper/lower case combinations of plaintext and key.

You must log in to answer this question.

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