# vigenere cypher issues with embedding loops

So I have two for loops, one to run through the key word and one to run through the plaintext. Thing is, when I put the first one on top of the second, the second only uses the last result of the first...so if I use the keyword bacon, it only uses the letter n. I have tried to embed them every way I can think of and nothing is working. I just get different wrong results. like using, for example, the "b" for all of the text, then the a, then the c, and so forth. Is there any way to just use one loop? Or am I missing some way to embed the two I have?

You can nest loops by writing one within the other.
For example, the next piece of code prints the phrase `"hello, world!"` n x m times

``````// often known as an outer loop
for (int i = 0; i < n; i++)
{
// often known as a nested loop
for (int j = 0; j < m; j++)
{
printf("hello, world!\n");
}
}
``````

What you need to do in vigenere is that you should cipher each character in the plaintext (e.g., `"Meet me at the park at eleven am"`) using the current key from the keyword (e.g., `"bacon"`).

Logically, we need to iterate over each of the characters in `plaintext` exactly once to cipher it. One way to do that is by writing something like

``````for (int i = 0, length = strlen(plaintext); i < length; i++)
{
// do something
}
``````

Now, for each character of these, we need to use the current key from the `keyword` to cipher it. And we don't really need to use a second loop for that!

We can have a variable to store the `index` of the current key in the `keyword` (e.g., `0` in case it's `b` or `2` in case it's `c` considering `"bacon"`). Then we can cipher our current character in `plaintext` using `keyword[index]`, but we'll have to care about `index` exceeding `keyword`'s boundaries.

You may have a look at this answer to get some sort of an idea of how you can take handle that!

Update: here's some pseudocode to make things easier for you

``````for (int i = 0, pl = strlen(plaintext), kl = strlen(keyword), index = 0 to i < pl, i++)
{
declare a char named current and set it to plaintext[i]

if (current is an alphabetical char)
{
if (current is a lowercase char)
{
cipher current using keyword[index % kl] appropriately
}
else
{
cipher current using keyword[index % kl] appropriately
}
increment index by 1
}

print current

}
``````
• I understand everything exempt the last bit. I could have a variable store a character like: – Sofia Jul 2 '14 at 1:01
• @user1353 I just updated my answer with some pseudocode for you to make it clearer! – Kareem Jul 2 '14 at 1:14
• sorry I accidentally cut off my comment. I still don't understand the "index keyword" part. are you doing it in the pseudocode? – Sofia Jul 2 '14 at 1:31
• ok I see its in the "index = o to i < pl" part. would you mind expanding a little? and how are you turning a letter into the right number? – Sofia Jul 2 '14 at 1:37
• @user1353 well, `index = 0` (i.e., zero). `pl` is the plaintext length and it's calculated via the function `strlen()`. Similarly, `kl` is the keyword length. – Kareem Jul 2 '14 at 1:39