0

My code for Vigenere is compiling but when I try to print out the Vigenere cipher for a word, the print out is alot longer then the word i prompted. I guess I am doing something wrong with my formula.

cipher[i] = ((current - 'a') + (keyword[j] - 'a') % kwlength)) % 26 + 'a';

kwlength = keywordlength

I think the problem is in the keyword formula:

(keyword[j] - 'a') % kwlength

For the upper case letters I use the same formula accept i am using 'A' instead of 'a'. I don't know how to fix it. Anyone got an idea about what I am doing wrong.

Description of my code:

  1. A for loop which is starting with i = 0 and working till i is the length of my message.

    1. Inside this for loop another for loop with j = 0 and it is checking the length of the keyword.

      1. An if statement which is checking if the array of chars from the keyword are all alphabetics (with isalpha).
      2. Then I used another if statement for to check if the message is containing alphabetics. Within this if statement I am checking for lower case and upper case alphabetics.

if (isalpha(message[i]))

        {
            // Checking lower case alphabetics.
            if (islower(message[i])) 
            {
                cipher[i] = ((message[i] - 'a') + (keyword[j % kwlength] - 'a')) % 26 + 'a'; 
                printf("%c", cipher[i]); 
            }
            // Checking upper case alphabetics.
            else if (isupper(message[i])) 
            {
                cipher[i] = ((message[i] - 'A') + (keyword[j % kwlength] - 'A')) % 26 + 'A';
                printf("%c", cipher[i]);
            }    
        }    
1
  • I used two nested for loops indeed. One with [i] for the length of the message and the other one with [j] for the length of the keyword. Do you think I shouldn't do that? If my answer is too long should I place a part of my code in a new function?
    – Kiwi
    Oct 21, 2015 at 19:18

1 Answer 1

2

If the output of the program is too long, it's more likely due to incorrect control flow. Maybe i, which appears to be the index of the output array is going out of range?

Your gut instinct about the keyword formula is probably right, as well, though. As it stands it means to find the jth letter in the key, figure out its numeric value, and then take the remainder when that numeric value is divided by the length of the keyword. If keyword is "bacon" and j is 4, this would mean:

  • find keyword[4]
    -result:'n' (110)
  • subtract 'a' (97) from 'n'
    -result: 13
  • take the remainder when 13 is divided by the length of bacon (5)
    -3

It seems more likely you want to do this:

  • find the remainder when j is divided by the length of keyword ensuring we wrap through the keyword.
    -result: the index of the letter in the keyword we will use this time

It'll look something like this:

cipher[i] = ((current - 'a') + (keyword[j % kwlength] - 'a')) % 26 + 'a';

I hope this gets you closer to an answer.

Update: As for why the length of the output is incorrect, it's very hard to say anything specific without you sharing some information about the control flow of your program. What I can say is that you shouldn't have to use nested loops. Here's an idea of a method that should work:

  • Make a variable called j and start it at 0. This will indicate the letter in the key.
  • Make a for loop for an index we'll call i. This is the letter in the originaltext.
  • Each time through the for loop, check the character at originaltext[i] and see if it's alphabetical (not a space).
    • If it is, encrypt the letter (using the code you showed above) and increment j, so the next time we use a new letter from the key.
    • If it isn't, don't do anything.

There are probably other ways to do this, but I think this a popular approach.

2
  • What can I do about the fact that the output of my program is to long? Should I not use a nested for loops? How can I make my program smaller?
    – Kiwi
    Oct 21, 2015 at 19:35
  • I added some vague pseudocode, but without more information about your program, there's nothing to say that can't be found in the walkthrough or spec.
    – Sam Gerber
    Oct 21, 2015 at 20:03

You must log in to answer this question.

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