I'm working on at least getting the key to apply the first go-around, then I will worry about how to wrap around the key. I'm outputting integers in the alphabetical index to keep it simpler.
Here is my expected input and output:
./vigenere AbC
plaintext: He Llo
expected: 75 131115
current: 743323846
So, it looks like the first letter works, then I am confused about what is happening with the next two letters in the plaintext. Also, my method to skip over the spaces used to work, now it doesn't anymore. Any hints on where my logic is failing? Thanks so much!
Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main (int argc, string argv[]) {
string k = argv[1];
int cipher_upper;
int cipher_lower;
int i;
int j;
int p_upper;
int p_lower;
int ascii_A = 65;
int ascii_a = 97;
if (argc != 2) {
printf("Please enter only one command line argument.\n");
return 1;
}
for (int c = 0; c < strlen(k); c++) {
if (!isalpha(k[c])) {
printf("Usage: ./vigenere k\n");
return 1;
}
}// end for loop to check for alpha characters only in key
printf("plaintext: ");
string p = get_string();
int p_len = strlen(p);
// int k_len = strlen(k);
//iterate through characters in plaintext
for (i = 0, j = 0; i < p_len; i++) {
//skips over non-alpha characters, ciphers uppercase characters
if (isalpha(p[i])) {
if (isupper(p[i])) {
p_upper = p[i] - ascii_A;
if (isupper(k[j])) {
cipher_upper = p_upper + ((k[j] - ascii_A));
j++;
printf("%i", cipher_upper);
}
if (islower(k[j])) {
cipher_upper = p_upper + ((k[j] - ascii_a));
j++;
printf("%i", cipher_lower);
}
} // end is upper plaintext
if (islower(p[i])) {
p_lower = p[i] - ascii_a;
if (isupper(k[j])) {
cipher_lower = p_lower + ((k[j] - ascii_A));
j++;
printf("%i", cipher_lower);
}
if (islower(k[j])) {
cipher_lower = p_lower + ((k[j] - ascii_a));
j++;
printf("%i", cipher_lower);
}
} // end is lower plaintext
} //end is alpha
else {
printf("%c", p[i]);
}
} //end for loop plaintext
} //end main