0

I have been banging my head as to why when I run vigenere with bacon argument, Meet me at the.....etc, never turns out the same as on the CS50 page, but always the same encryption values on my side. However, my letters are encrypting through the key and case is being kept as input by the user.

After spending hours trying to modify and change my code, I just counted the letters to encryption and realised my cipher is adding an extra 19 places on the key index? I have no clue why.

I modified my cipher to subtract the 19 and it works. I have run various tests and multiple variations and my cipher seems to work correctly each time. My question is, where on earth did this 19 come from?

Thanks in advance.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

int cipher(int i, char k);

int main(int argc, string argv[])
{
    if (argc != 2)
    {
    printf("Usage: ./vigenere (key alphabetical only)\n");
    return 1;
    }

    for (int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if (!isalpha(argv[1][i]))
        {
        printf("Usage: ./vigenere (key alphabetical only)\n");
        return 1;
        }
    }

    string s = GetString();   
    string key = argv[1];
    int kl = strlen(key);
    int k = 0;

    for (int i = 0, n = strlen(s); i < n; i++)
    {
        if (isalpha(s[i]))
            {
                printf("%c", cipher(s[i], key[k%kl]));
                k++;
            }
        else
        printf("%c", s[i]);
    }
    printf("\n");
    return 0;
}
int cipher(int c, char k)
{   
    if (islower(c)) return (c -'a' + k - 19) % 26 + 'a';
    if (isupper(c)) return (c -'A' + k - 19) % 26 + 'A';
    return c;
}
0

The problem lies in your formula for encoding.

         (c -'a' + k - 19) % 26 + 'a';

You recognized that you need to subtract the base value, 'a' from the letter to be encoded, but you forgot to do the same with k. The formula (k - 19) % 26 happens to have the same effect as (k - 'a'). That's why it works.

However, have you checked the cases where the key letter and the letter to be encoded are of different case? I suspect that it will fail.

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

8
  • I just realised this too, I have changed the formulae to this: int cipher(int c, int k) { if (islower(c)) return ((c -'a') + (k - 'a')) % 26 + 'a'; if (isupper(c)) return ((c -'A') + (k - 'A')) % 26 + 'A'; return c; }
    – AdenJ
    Nov 28 '15 at 21:45
  • But just as you suspected, the upper case encryption is not following the key correctly, it is supplementing with 7 characters?
    – AdenJ
    Nov 28 '15 at 21:47
  • Don't know what "supplementing with 7 characters means", but I suspect that the result is a shift of 7 characters away from what it should be. So, say that your letter to encode is 'a', and your key is 'A'. What do you get from k = 'A' - 'a'?
    – Cliff B
    Nov 28 '15 at 21:48
  • Yes. That is what I meant sorry. I am so lost now. Why would the lower case encrypt correctly yet the upper case not? Let me check
    – AdenJ
    Nov 28 '15 at 21:50
  • Try this: key = "AAaa" and plaintext to encode = "AaAa". You might catch on. ;-) If you don't, then start working it out with pencil and paper, replacing letters with numbers.
    – Cliff B
    Nov 28 '15 at 21:50

You must log in to answer this question.

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