0

I have the following relevant code for encrypting in the Vigenere cipher: (Note: I created a function called shift that shifts a character by a certain amount.) int main (int argc, string argv[]) {

if (argc != 2) {
    printf("over\n");
    return 1;
}    
else {
    string plain_text = GetString();
    string key = argv[1];
    string new_text = malloc(strlen(plain_text));
    int keylength = strlen(key);
    int lenfroma[keylength];
    for (int x = 0; x < strlen(key); x++) {
        int keyx = (int)tolower(key[x]);
        int keya = 97;
        lenfroma[x] = keyx-keya;
     //   printf("len from a first is %i\n", lenfroma[x]);
    }
    int num_of_space = 0;
    for (int m = 0; m < strlen(plain_text); m++) {
        if (m < strlen(key)) {
        new_text[m] = shift(plain_text[m],lenfroma[m]);
     //   printf("doing 1, new text m is %c\n", new_text[m]);
        if (new_text[m] == ' ') {
            num_of_space += 1;
        }
        }
        else {
            int a = strlen(key);
            int realm = m - num_of_space;
            int x = (realm%a);
            new_text[m] = shift(plain_text[m],lenfroma[x]);
         //   printf("len from a is %i\n", lenfroma[x]);
          //  printf("doing 2, new text m is %c\n", new_text[m]);
            if (new_text[m] == ' ') {
                num_of_space += 1;
            }
        }
    }
    for (int m = 0; m < strlen(new_text); m++) {
        printf("%c",new_text[m]);
    }
    printf("\n");

} } When I run "check50 2015.fall.pset2.vigenere vigenere.c", the output is at: https://sandbox.cs50.net/checks/a26e544f5f354bbe9b9f585a14b8effc

The main error that I am confused by is :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "xoqmd, tax iekmo!\n"

All of the other outputs are returned accurately. Is there anything wrong with my logic? Thanks in advance.

1

Interesting design that (mostly) works when using the same index to track both the plaintext and the key by also tracking (mostly) when something is not encoded.

It's failing the one test because of the comma in the plaintext. More generally speaking, the code is only handling alphas and spaces, when it should be handling all non-alphas. Since it doesn't handle punctuation, special characters, numbers, etc., the key will get out of synch. If you'd like to test, run any plain text with only alphas and spaces through your code. Then, run the same string through, but insert some other non-alpha, non-space characters in the string and compare the outputs. If the program were correct, the encoded letters would be the same. If they change, it shows the problem still exists.

BTW, are you familiar with the function isalpha()? it could come in handy.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Thanks so much! This really helped :) – jaguar Feb 23 '16 at 23:30
  • Yes I did use isalpha() for my program just now - check50 now returns correct. – jaguar Feb 23 '16 at 23:34

You must log in to answer this question.

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