I am currently working on Vigenere. I have the code to where it will work if the key is very short (a few characters), but for some reason my program bums out when the plaintext is long or the key is long. Where could I be going wrong? Thank you all so much and have a great day.
// 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);
int j = 0;
// Finds the keys
for (int i = 0; i < plainlen; i++)
{
if (j == keylen)
{
j = 0;
}
keys[j] = shift(keyword[j]);
// Prints the Ciphertext
char printk = (plaintext[i] + keys[j] % 26);
printf("%c", printk);
j++;
}
printf("\n");
}
int shift(char c)
{
if (islower(c))
{
return (c - 'a');
}
else if (isupper(c))
{
return (c - 'A');
}
else {
return 0;
}
}