I am having an issue specifically with the second for loop (under "//iterate over key inputs" comment). The counter j is going over the strlen of argv[1], and thus feeding a wrong input to my shift function. So I am getting shifts greater than the ones corresponding to the key input (A, a = 0, B, b = 1, etc.).
I tried to fix this by placing a conditional in the loop, to check if j == m-1, then return j to 0. However this is not doing anything and I am not sure why.
Last issue is that the program is not printing non alphabetical characters in the ciphertext, even though I put a conditional for that function at the end of the code.
Thanks in advance.
int shift(char c);
int main(int argc, string argv[])
{
char c;
int key;
string p[1];
if (argc != 2)
//if input is not 2 strings, print the following and return 1 to end program
{
printf("Usage: ./vignere keyword\n");
return 1;
}
else
{
int i = 0;
int j = 0;
int n = 0;
int m = 0;
if (isalpha(argv [1][j]) == false)
{
printf("Usage: ./vignere keyword\n");
return 1;
}
p[0] = get_string("plaintext: ");
printf("ciphertext: ");
//loop to iterate over each character of key input string
for (i = 0, n = strlen(p[0]); i < n; i++)
{
//printf("i: %i", i);
//check if plaintext input is a letter, if so, then...
if (isalpha(p[0][i]))
{
//iterate over key inputs
for (j = i, m = strlen(argv[1]); abs(j-i) < 1 ; j++)
{
//printf("j = %i", j);
//check if key input is alphabetic, if true, then...
if (isalpha(argv [1][j]))
{
c = argv [1][j];
//set int key as result of function shift
key = shift(argv[1][j]);
c = (p[0][i] + key);
//printf("k: %i", key);
printf("%c", c);
}
if (j == m-1)
{
j = 0;
}
}
if (isalpha(p[0][i]) == false)
//if plaintext is not a letter, print as is
{
printf("%c", p[0][j]);
}
}
}
}
}
int shift(char c)
{
if (isupper(c))
{
return c - 65;
}
return c - 97;
}