Vigenere cipher: how to loop the key

Please help me with my code below. I cannot seem to get the argv (the key) to loop around plaintext. For example, if I my key is "bacon", and the plaintext is "Meet me at the park", I only get "Negh z". I tried to use modulo to loop, e.g., k % strlen(argv), but that does not seem to work.

Here is part of my code: (I excluded the parts for lowercase k, uppercase n, and for spaces)

for (int i = 0, n =0; n < strlen(plaintext); n++)
{
char k = argv[i];

//for uppercase k
if (isupper(k))
{
if(isalpha(plaintext[n])
{

//for lowercase n
if(islower(plaintext[n]))
{
printf("%c", ((((plaintext[n] - 'a') + k) % 26) + 'a'));
}
i++;
}
}
}

First of all it might be better for you if you assigned argv to a string variable called keyphrase for example:

char* keyphrase = argv;

Now say keyphrase is "bacon". It would be like that:

• keyphrase == 'b'
• keyphrase == 'a'
• keyphrase == 'c'
• keyphrase == 'o'
• keyphrase == 'n'
• keyphrase == '\0'

and if you do

int length = strlen(keyphrase);

length would be 5.

So let's go through the loop you have to go:

• i == 0, k should be keyphrase
• i == 1, k should be keyphrase
• i == 2, k should be keyphrase
• i == 3, k should be keyphrase
• i == 4, k should be keyphrase
• i == 5, k should be keyphrase
• i == 6, k should be keyphrase
• ... and so on.

So see that you should somehow use the modulo operator on the index of keyphrase and assign that to k. You shouldn't use the modulo on k itself.

I think this should get you going. If not, or if I've confused you more, leave a comment below. On the other hand, if this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!