My code works properly for the Vigenere problem, but only if the key and the plaintext have the same number of letters in them. I think the problem is in the loops I'm trying to implement, but I'm not sure where to go from here.
Here's the code:
#include <cs50.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main (int argc, string argv [])
{
if (argc !=2)
{
printf ("Error. Instructions not followed.\n");
return 1;
}
string key = argv[1];
for (int i = 0, n = strlen (key); i < n; i++)
{
if (!isalpha(key[i]))
{
printf ("Non-alphabetical characters.\n");
return 1;
}
}
printf ("plaintext: ");
string plaintext = get_string();
printf ("%s\n", plaintext);
printf("ciphertext:");
for (int g = 0, k = 0, z = strlen (plaintext), v = strlen (key); g < v, k < z; g++, k++)
{
if (isalpha(plaintext[k]) && isalpha(key[g]))
{
if (isupper(plaintext[k]) && isupper(key[g]))
{
printf("%c", (((((plaintext[k])-65)+(key[g]-65))%26)+65));
}
}
}
}
Any help would be appreciated. Thanks in advance.