I am stuck on this problem for days, and I have no idea about where went wrong! When I typed in /vigenere ABC the ciphertext of HELLO just becomes HFN?Z.
PLEASE HELP ME!
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
//make sure the the key is entered and is 1 word
if (argc != 2)
{
printf("Error. Try Again. \n");
return 1;
}
string key = (argv[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++)
{
if (isalpha(text[i]))
{
//check if each character is lowercase
if islower(text[i])
{
printf("%c", ((((text[i] - 'a') + tolower(key[j]) - 'a') % 26) + 'a'));
j++;
}
else if (isupper(text[i]))
{
printf("%c", ((((text[i] - 'A') + toupper(key[j]) - 'A') % 26) + 'A'));
j++;
}
}
// returns all non-alphabet value
else
{
printf("%c", text[i]);
}
}
printf("\n");
return 0;
}