0

OK, I have tried everything, but there still seems to be something wrong with my code for vigenere cipher. I mean I can't get it to loop through the keyword when the word is longer than it. I swear I have tried everything, but it just doesn't seem to work. Since I don't want to post my code here, would someone be willing to have a private message from me with my code in order for debugging? Thanks.

3
  • Shoot me a pm. I'd be happy to help
    – lethaljd
    Commented Aug 11, 2014 at 11:04
  • There is no private message system on Stack Exchange; if you can't identify the section of the code that is causing problems, you might be better off looking for one-on-one help on the subreddit which does have private messages.
    – Air
    Commented Aug 11, 2014 at 15:49
  • possible duplicate of How to Wrap Around Array Indexes?
    – lethaljd
    Commented Sep 4, 2014 at 16:58

1 Answer 1

4

In order to loop through the keyword, you need to use the modulo operator.

For example, say your keyword is 5 characters long, while the text-to-be-enciphered is 10 characters long. At the beginning of your code you will have some for loop like this:

for (int i = 0, n = strlen(plain), k = 0; i < n; i++, k++)

Here, k is used to access consecutive characters of your argv[1] key. However, in order to loop through your keyword, you cannot use k, but need to use the modulo, like this:

int j = k % strlen(argv[1]); // ensure looping of key

The modulo divides your loop number by your key length, and as a result gives you the remainder of the result. This remainder can never be longer than the number you are dividing by, and therefore you will only get numbers from 0 to 4 (in the case of your keyword being 5 characters long).

Now, you can use argv[1][j] to access the consecutive characters of your key in a loop.

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