0

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
1

I really don't know where your numbers start and end, and what they refer to (I don't think that's the output of the code you show), at least some whitespace between would have been nice. Also, the non-alpha part seems to work fine.

You need to apply some wrap-around. If shifting a 'Z' further forward, it should become 'A' again. You can achieve this by applying %26. After that, you'd have to add 'A' or 'a' again, to shift the number of range 0..25 back to a character.

You already realized that j needs a bit of additional handling. You could use % here, or various other ways.

Also, a final newline character after the loop is probably required.

And you could have saved a few lines by using toupper(k[j]) - 'A' instead of one version for upper- and one for lower-case k[j].

1
  • Thanks so much! And sorry if my code was confusing to look at. I think I was staring at it for too long. Back on track now. – Eliana Haig Nov 12 '17 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .