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");
}