I have debugged my program throughout and it compiles perfectly. The issue I have though is the it doesn't give me the output I expected. Im creating a vigenere cipher in c and im pretty sure that you all know what its suppose to do. Heres the error :
When I type in the key bacon as a command line argument and the message i want to deliver is Meet me at the park at eleven am, I should get Negh zf av huf pcfx bt gzrwep oz. Instead what i get is
I don't know why this is occuring but here's my code below, and if someone can just give me a hint on how to fix the problem then I would really appreciate it.
Thank you, Shayan.
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <cs50.h>
int main (int argc, string argv[])
{
string plaintext = GetString();
int plaintext_length = strlen(plaintext);
string key = argv[1];
int key_length = strlen(key);
int shift[key_length];
if (argc != 2) {
printf("The command is not valid and cannot be used as a key to encrypt your message\n");
return 1;
}
for (int i = 0; i < key_length; i++)
{
if (!isalpha(key[i]))
{
printf("The command is not valid and cannot be used as a key to encrypt your message\n");
return 1;
}
}
for (int i = 0; i < key_length; i++)
{
if (isupper(key[i])) {
shift[i] = key[i] - 65;
} else {
shift[i] = key[i] - 97;
}
}
for (int j = 0, i = 0; j < plaintext_length; j++)
{
if (islower(plaintext[j]))
{
printf("%c", (plaintext[j] + shift[i]) % 26);
} else if (isupper(plaintext[j]))
{
printf("%c", (plaintext[j] + shift[i]) % 26);
}
}
printf("\n");
return 0;
}