Having some trouble with pset2 vigenere. Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
// return error if there is an invalid number of arguments
printf("Error! Ensure there are exactly 2 command line arguments: './caesar' and the key!\n");
return 1;
}
else
{
// otherwise continue with the code
string key = argv[1];
// get plaintext
string plaintext = GetString();
// character length of plaintext variable
int ptLen = strlen(plaintext);
int cypherText[ptLen];
for (int i = 0; i < ptLen; i++)
{
// check if letter is part of the alphabet
if (isalpha(plaintext[i]))
{
// check if letter is upper case
if (isupper(key[i]))
{
// formula for encrypting upper case text
cypherText[i] = plaintext[i] + ((key[i] - 65) % 26) ;
}
// check if letter is lower case
if (islower(key[i]))
{
// formula for encrypting lower case text
cypherText[i] = plaintext[i] + ((key[i] - 97) % 26) ;
}
}
printf("%c", cypherText[i]);
}
printf("\n");
return 0;
}
}
I know I need to wrap around my key to the plaintext so it repeats itself to the length of the plaintext instead of just encrypting to the length of key once. Ia also need to use a different index for the key. I am very confused. I have thought about how to do this but cannot come to a working solution. Any help on how I could do this is appreciated! Thanks! =D