I'm working on Vigenere.c, and while my code seems to be mostly correct, I keep having very strange outputs which are not even the same every time I run the program (e.g. If I run my code one time with keyword = 'a' and message = 'a' I will get an output different then when I run that same code again also with keyword = 'a' and message = 'a'). Can anyone help me with this? I'm not sure what is going wrong/where to start...
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// description
int z;
int m;
int found = 0;
int main(int argc, string argv[])
{
if (argc == 2)
{
string keyword = argv[1];
m = strlen(keyword);
for(z = 0; z < m; z++)
{
if (isalpha(keyword[z]))
{
continue;
}
else
{
found = 1;
break;
}
}
if (argc == 2 && found == 0)
{
string message = GetString();
int n = strlen(message);
char encrypt[n];
int j = 0;
for(int i = 0; i < n; i++)
{
if (isalpha(message[i]) && islower(message[i]) && islower(keyword[i - j]))
{
encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] - 'a')) % 26) + 'a';
}
else if (isalpha(message[i]) && islower(message[i]) && isupper(keyword[i - j]))
{
encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] - 'A')) % 26) + 'a';
}
else if (isalpha(message[i]) && isupper(message[i]) && islower(keyword[i - j]))
{
encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] - 'a')) % 26) + 'A';
}
else if (isalpha(message[i]) && isupper(message[i]) && isupper(keyword[i - j]))
{
encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] - 'A')) % 26) + 'A';
}
else
{
encrypt[i] = message[i];
j++;
}
}
printf("%s", encrypt);
}
else
{
printf("try again");
return 1;
}
}
else
{
printf("error");
return 1;
}
}