I am having trouble with vigenere; I can't loop the argv[] with the string[] and then starting again.

  • Possible duplicate of: How to Wrap Around Array Indexes?
    – kzidane
    Aug 3 '14 at 13:25
  • I thought that we cannot read previous post or anything on internet is this reasonable and by the way it is sort of working now just need final touches .
    – Rajat
    Aug 3 '14 at 13:33
  • Well, you should search for hints if you're having a problem that you can't solve. I assume this question is about the same problem you have and it has some answers that might be useful for you!
    – kzidane
    Aug 3 '14 at 13:36

vigenere takes the encryption key as a command-line argument. That key is stored in argv[1] (because argv[0] contains the name of the program -- ./vigenere).

To get the length of the key, you should use the strlen() function (declared in string.h). You should then create an int variable to store the index of the current character in the key that we're gonna use to encrypt our plain text. This variable should be initially set to 0 (since argv[1][0] is the first character in the key).

You should then increment this index by 1 each time you encrypt an alphabetical char in the plain text.

If the length of the plain text is greater than the length of the key, eventually, index will be equal to strlen(argv[1]) which is the length of the key and that's an invalid index (because it's beyond the array boundaries).

In this case, we'll need to do one of two things to get index back to 0.

  1. to use an if statement

    if (index >= keyLength)
        index = 0;
  2. to use the remainder operator

    index = (index + 1) % keyLength

    after you've used index at least once or as a shortcut, encrypt using argv[1][index++ % keyLength]

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