Substitution Cipher: Which One? I'm a beginner and I have a question (somehow silly and stupid :) ).  Today I decided to challenge myself and I came around the challenge that wanted me to create a program that ciphers (or encrypts) the message using the substitution cipher method.  I solved the challenge by myself but mine is way different than the solution itself. I just want to know which one is better and why? And also is there anything I missed in my own code?

So here is the code I've written:

#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;
int main()
{
    string secretMessage {};
    string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
    cout &lt;&lt; "Enter your secret message: ";
    getline(cin, secretMessage);
    //Encryption
    for(size_t i{0}; i&lt;secretMessage.length(); ++i){
        for(size_t j{0}; j&lt;alphabet.length(); ++j){
            if (secretMessage.at(i) == alphabet.at(j)){
                secretMessage.at(i) = key.at(j);
                break;
            }
        }
    }
    cout &lt;&lt; "Encrypting The Message..." &lt;&lt; endl;
    cout &lt;&lt; "Encrypted Message: " &lt;&lt; secretMessage &lt;&lt; endl;
    //Decryption
    for(size_t i{0}; i&lt;secretMessage.length(); ++i){
        for(size_t j{0}; j&lt;key.length(); ++j){
            if (secretMessage.at(i) == key.at(j)){
                secretMessage.at(i) = alphabet.at(j);
                break;
            }
        }
    }
    cout &lt;&lt; "\nDecrypting The Encryption..." &lt;&lt; endl;
    cout &lt;&lt; "Decrypted: " &lt;&lt; secretMessage &lt;&lt; endl;
    return 0;
}


And here is the solution:


#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;
int main()
{
    string secretMessage {};
    string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
    string encryptedMessage {};
    string decryptedMessage {};
    cout &lt;&lt; "Enter your secret message: ";
    getline(cin, secretMessage);
    cout &lt;&lt; "\nEncrypting Message..." &lt;&lt; endl;
    //Encryption
    for(char c:secretMessage){
        size_t position = alphabet.find(c);
        if (position != string::npos){
            char newChar {key.at(position)};
            encryptedMessage += newChar;
        } else{
            encryptedMessage += c;
        }
    }
    cout &lt;&lt; "Encrypted Message: " &lt;&lt; encryptedMessage &lt;&lt; endl;
    //Decryption
    cout &lt;&lt; "\nDecrypting Message..." &lt;&lt; endl;
    for(char c:encryptedMessage){
        size_t position = key.find(c);
        if (position != string::npos){
            char newChar {alphabet.at(position)};
            decryptedMessage += newChar;
        } else{
            decryptedMessage += c;
        }
    }
    cout &lt;&lt; "Decrypted Message: " &lt;&lt; decryptedMessage &lt;&lt; endl;
    return 0;
}



Note:I have also included the decryption part too
Avoid using namespace std

It's a bad idea to import large namespaces such as std into the global namespace, and it could possibly even change the meaning of your code when a later standard adds new identifiers.  Reserve using namespace for those few namespaces specifically designed to be used that way (notably std::literals).

Use const appropriately

We don't want alphabet or key to be modified, so prevent accidents by declaring them as const std::string.

getline() can fail

If the input stream is closed, getline() will read nothing, and the string will be empty.  That's probably acceptable for this program, but probably worth a comment to show that you've considered this.

Use range-based for

We have an integer loop whose value is used only for indexing into the string:


for(size_t i{0}; i&lt;secretMessage.length(); ++i){
    // use secretMessage[i] here



That's easier to read with modern range-based syntax:

for (auto const c: secretMessage) {
    // use c here


Use standard algorithms

We can include &lt;algorithm&gt; to get some really useful functions from the standard library.  In particular, std::find() could replace the loop that searches in the alphabet.

Reduce duplication

Notice that there are two blocks of code that are almost identical, due to the symmetric nature of this cipher.  The only difference is that the role of alphabet and key are swapped.  This makes it a good candidate to extract as a function:

std::string substitute(const std::string&amp; message,
                       const std::string&amp; from,
                       const std::string&amp; to)


Then we could just call it:

auto encrypted = substitute(secretMessage, alphabet, key);
auto decrypted = substitute(encrypted, key, alphabet);


Avoid linear search

We can be more efficient with our lookup than simple linear search.  For example, we could construct a pair of std::map&lt;char,char&gt; from the alphabet and key.

Clean up the plaintext

I know this is just toy encryption, but for real code, we'd want to overwrite the decrypted text when we finish using it, to reduce the ability for an attacker to access it (e.g. from a core dump).  Remembering &lt;algorithm&gt;, we'd use std::fill() for this.

Omit the return

In main() (and only there), we can omit the return statement and the compiler will automatically return 0 for us.  It's a fairly common convention to omit the return if we don't have any non-zero (i.e. error) returns from main().