-1

I am taking Harvard's CS50 course through EDX (just for myself, this is not graded work). My Vigenere cipher from PSET2 is giving the wrong output - for example, both a key and an input of a should result in an output of a, but instead gives t. However, I can't pinpoint where the problem is.

#import <stdio.h>
#import <cs50.h>
#import <string.h>
#import <ctype.h>

int main(int argc, char *argv[])
{
    //Variables
    string key;
    key = argv[1];
    string plainText;
    plainText = argv[2];
    int i;
    int k;
    i = 0;
    k = 0;


    //Encrypt the string
    for (i = 0; i < strlen(plainText); i++)
    {
        if (isalpha(plainText[i]))
        {
            if (islower(plainText[i]))
            {
                printf("%c",plainText[i] - 97 + key[k]  % 26 + 97);
                k++;                   
            }   

            if (isupper(plainText[i]))
            {
                printf("%c",plainText[i] - 65 + key[k] % 26 + 65);
                k++;
            }
        }

        else 
            printf("%c",plainText[i]);
    }
    printf("\n");
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Eric
  • 3
  • 2

3 Answers3

1

If the values of the key array are supposed to represent the cyclic shift value (with aA standing for zero shift, bB - shift of 1 and so on) then the encoding expression should look as follows

(plainText[i] - 97 + key[k] - 97) % 26 + 97

Of course, in that case you have to independently take into account the case of key[k] character as well (and subtract either 97 or 65 from key[k]), which you ignore completely at this time.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

Well, I can't provide a complete answer because I'm also struggling with this one. However, a couple of things I noticed from your code was that your plainText is argv[2] but there should only be 2 arguments passed through command line (argv[0] (the name of the program) and argv[1] (the users key)). For the plainText you should use GetString() from the cs50 library.

The other thing I noticed was that although you use the key[k] in your printf(), you aren't iterating through it in the loop. I am also having difficulty with this part. So, unfortunately I can't be certain of this part. I think you probably need a loop to go through each letter of the key in order to make a/A = 0 and z/Z = 25.

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
Claire
  • 1
0

Don't forget that your key[] letter values start at 97 (for lowercase) or 65 (for uppercase) - you need to adjust them for the mod 26 operation to make sense.

And keep in mind that the mod operator (%) has a higher precedence than addition and subtraction; you might need to use parens to have it apply to the correct sub-expression.

I haven't looked at the requirements of the assignment, so I'm not sure if the program is expected to handle the possibility of key characters being uppercase or lowercase (or even non-alpha), if so you will need to add some logic to handle that complexity. Also, the way the program is coded right now, if your key is shorter than the plaintext you'll have problems - I assume your code is expected to handle 'wrapping' the key as necessary.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760