Skip to main content
added 77 characters in body
Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143

I think I'd start by splitting it into two pieces, one to encrypt and one to decrypt. As a user, I'd much rather type decrypt [whatever] or 'encrypt [whatever]`encrypt [whatever] than have to interactively enter an 'e' or 'd' to tell it whether to encrypt or decrypt.

Likewise, each would act as a filter, taking its input from cin, and writing its output to cout (or possibly read/write other files if you specify them on the command line). Again, as a user I'd much rather be able to take input from a file and/or write output to a file (and this still allows me to run it interactively in the rare circumstance that I really want to).

I think I'd start by splitting it into two pieces, one to encrypt and one to decrypt. As a user, I'd much rather type decrypt [whatever] or 'encrypt [whatever]` than have to interactively enter an 'e' or 'd' to tell it whether to encrypt or decrypt.

Likewise, each would act as a filter, taking its input from cin, and writing its output to cout. Again, as a user I'd much rather be able to take input from a file and/or write output to a file (and this still allows me to run it interactively in the rare circumstance that I really want to).

I think I'd start by splitting it into two pieces, one to encrypt and one to decrypt. As a user, I'd much rather type decrypt [whatever] or encrypt [whatever] than have to interactively enter an 'e' or 'd' to tell it whether to encrypt or decrypt.

Likewise, each would act as a filter, taking its input from cin, and writing its output to cout (or possibly read/write other files if you specify them on the command line). Again, as a user I'd much rather be able to take input from a file and/or write output to a file (and this still allows me to run it interactively in the rare circumstance that I really want to).

Source Link
Jerry Coffin
  • 33.6k
  • 4
  • 75
  • 143

I think I'd start by splitting it into two pieces, one to encrypt and one to decrypt. As a user, I'd much rather type decrypt [whatever] or 'encrypt [whatever]` than have to interactively enter an 'e' or 'd' to tell it whether to encrypt or decrypt.

Likewise, each would act as a filter, taking its input from cin, and writing its output to cout. Again, as a user I'd much rather be able to take input from a file and/or write output to a file (and this still allows me to run it interactively in the rare circumstance that I really want to).

For implementation of the cipher, I'd consider using a little math instead of the tables you're currently using. For example, encrypting would look something like this:

int encrypt(char ch) { 
    int linear = ch - 'A' - (ch > 'I');
    int row = linear / 5 + 1;
    int column = linear % 5 + 1;
    return row * 10 + column;
}

Using this, a complete encryption program could look something like this:

int main() {
    int ch;
    while (EOF != (ch = getchar()))
        if (std::isupper((unsigned char)ch))
            std::cout << encrypt(ch) << ' ';
}

Likewise, decryption might look something on this order:

char decrypt(int coords) {
    int column = coords % 10 - 1;
    int row = coords / 10 - 1;
    int linear = row * 5 + column + 'A';
    if (linear > 'I')
        ++linear;

    return linear;
}

The decryption program using this would look something like this:

int main() {
    int ch;
    while (std::cin >> ch)
        std::cout << decrypt(ch);
}

I suppose some might take issue with: int linear = ch - 'A' - (ch > 'I'); Personally, I find it perfectly understandable, but some might prefer code more like:

int linear = ch - 'A';
if (ch > 'I')
    --linear;

The other point to consider is that in theory this is marginally less portable. In particular, it assumes that upper case characters are assigned a contiguous range of character codes, so 'B' = 'A' + 1, and so on throughout the alphabet. Although that's true with all sane character sets, there's one (EBCDIC) that actually breaks the alphabet into three pieces, and inserts some special characters between those pieces. At least in my opinion, however, this is sufficiently stupid that it deserves only ridicule. Catering your code to it would (in my opinion) be relatively silly as a rule.