I am currently working on Vigenere, and for whatever reason my key values are running through randomly and just not working. I'm new to programming, so sorry if I am missing something major in my code or if the format of the question is wrong. Here is my Vigenere code:
// CS50 Vigenere
// February 19, 2019
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
// Shift function
int shift(char c);
// Main Function
int main(int argc, string argv[])
{
// Ensures only one argument that is a string is entered as the key
if (argc < 2 || argc > 2)
{
printf("Error 1: Please enter one argument as the keyword.\nUsage: ./vigenere keyword\n");
return 1;
}
else if (atoi(argv[1]))
{
printf("Error 2: Please input a string as the keyword\nUsage: ./vigenere keyword\n");
return 2;
}
// Variables
string keyword = argv[1];
int keylen = strlen(keyword);
int keys[keylen];
string plaintext = get_string("Plaintext: ");
int plainlen = strlen(plaintext);
// Finds the keys
for (int i = 0; i < plainlen; i++)
{
for (int j = 0; j < keylen; j++)
{
keys[j] = shift(keyword[i]);
// Prints the Ciphertext
char printk = (plaintext[i] + keys[j] % 26);
printf("%c", printk);
if (j == keylen)
{
j = 0;
}
}
}
printf("\n");
}
int shift(char c)
{
if (islower(c))
{
return (c - 'a');
}
else if (isupper(c))
{
return (c - 'A');
}
else {
return 0;
}
}