I am getting the correct result from the CS50 Vigenere as long as argc does not equal to 1. When it does, it shows runtime error and therefore I am unable to submit this exercise. Can someone please tell me where did I do wrong?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
string key = (argv[1]);
int keylen = strlen(key);
//make sure the the key is entered and is 1 word
if (argc != 2)
{
printf("Error. Try Again. \n");
return 1;
}
for (int i = 0, n = strlen(key); i < n; i++)
{
if (!isalpha(key[i]))
{
printf("Error. Try Again.\n");
return 1;
}
}
//get plaintext from user
string text = get_string("Plaintext: ");
printf("ciphertext: ");
//create integer for key loop
for (int i = 0, j = 0, textlen = strlen(text); i < textlen; i++, j++)
{
if (isalpha(text[i]))
{
//check if each character is lowercase
if islower(text[i])
{
printf("%c", 'a' + ((text[i] - 'a') + (key[i % keylen] - 'a')) % 26);
}
else if (isupper(text[i]))
{
printf("%c", 'A' + ((text[i] - 'A') + (key[i % keylen] - 'A')) % 26);
}
}
// returns all non-alphabet value
else
{
printf("%c", text[i]);
}
}
printf("\n");
return 0;
}