So far in Vigenere i've gotten to the point with a single character key, and when I tried to test it, everything works except my output is entirely ����.

Here is my code for the shift function and where it actually applies it:

int shift(char c)
    int k = c;
    return k;
for(int i = 0; i < strlen(s);i++) // ensures the argument is alpha
            printf("Error: only alphabetic arguments allowed\n");
            return 1;
        int key = shift(argv[1][0]);
        printf("plaintext: %s\n", input);
        printf("ciphertext: ");
        for (int c = 0, n = strlen(input); c < n; c++)
            printf("%c", input[c]+key);

Here's the actual terminal output:

$ ./vigenere h
Message to be encrypted: hello
plaintext: hello
ciphertext: �����

Let's look at the calculation:

        printf("%c", input[c]+key);

This adds the ascii value of the plaintext letter to the ascii value of the key letter. This leads to an unprintable character. More specifically, since a char is also treated as a one byte signed integer (google twos complement), it will always result in a negative value, resulting in the same wierd character being printed.

You need to go back and review the lectures and all the video shorts related to this pset until you understand what's going on.

Also, while you get points for inplementing a function, the shift function above essentially does nothing except copying a 1 byte signed value to a 4 byte signed value and returning it. All the math can be done using char variables and treating them like the 1 byte signed values they are.

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

  • The function was actually part of the instructions for the assignment. I’m looking at this on my phone so I’ll check the other stuff tomorrow. Thanks for the reply! May 24 '19 at 4:14

So far, shift is just turning a char into a int, which explains the �'s.

shift is actually not shifting. The definition for shift probably should be something like this:

string shift(string text, int *key)

Where text is the text to be shifted and key is the list of the converted key (int * just means a list of type int). Because the key has to be converted, so you should also develop a convert function, which takes in the key (as a string) and returns an int * after processing it (turning it into lowercase and then into a number by subtracting 'a'). After that, shift will be easier to implement.

You must log in to answer this question.

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