C++ Decypher message

User Generated

FAF104440296657988927822

Computer Science

Description

How to Add the following 22 decryptions by looping through the cipherV vector, Instead of coding 22 more branch statements.

The complete list of cipher and normal characters to decrypt a message:

'!' > 'a' '^' >'b' '&' >'c' '*' >'d' '@' >'e' '(' > 'f' ')' > 'g' ''-" > 'h' '#' > 'i' '_' > 'j' '=' > 'k' '+' > 'l' '[' > 'm'

'{' > 'n' '$' > 'o' ']' > 'p' '}' > 'q' ';' > 'r' ':' > 's' ',' > 't' '%' > 'u' '<' > 'v' '.' > 'w' '>' > 'x' '/' > 'y' '?' > 'z'

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
vector<char> normalV(26);
vector<char> cipherV(26);
string toDec = "";
string beenDec = "";

normalV.at(0) = 'n'; cipherV.at(0) = '{';

// Get secret message
do {
cout << "Enter a secret message: ";
getline(cin, toDec);
} while (toDec.length() == 0);

beenDec = toDec;

// Decrypt secret message
if (toDec.at(0) == cipherV.at(0)) {
beenDec.at(0) = normalV.at(0);
}

cout << "Decrypted message: " << beenDec << endl;

return 0;
}

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    const int setNum = 26;
    vector<char> normalV(setNum);
    vector<char> cipherV(setNum);
    string toDec = "";
    string beenDec = "";
    int i = 0;

    normalV.at(i) = 'a'; cipherV.at(i) = '!'; ++i;
    normalV.at(i) = 'b'; cipherV.at(i) = '^'; ++i;
    normalV.at(i) = 'c'; cipherV.at(i) = '&'; ++i;
    normalV.at(i) = 'd'; cipherV.at(i) = '*'; ++i;
    normalV.at(i) = 'e'; cipherV.at(i) = '@'; ++i;
    normalV.at(i) = 'f'; cipherV.at(i) = '('; ++i;
    normalV.at(i) = 'g'; cipherV.at(i) = ')'; ++i;
    normalV.at(i) = 'h'; cipherV.at(i) = '-'; ++i;
    normalV.at(i) = 'i'; cipherV.at(i) = '#'; ++i;
    normalV.at(i) = 'j'; cipherV.at(i) = '_'; ++i;
    normalV.at(i) = 'k'; cipherV.at(i) = '='; ++i;
    normalV.at(i) = 'l'; cipherV.at(i) = '+'; ++i;
    normalV.at(i) = 'm'; cipherV.at(i) = '['; ++i;
    normalV.at(i) = 'n'; cipherV.at(i) = '{'; ++i;
    normalV.at(i) = 'o'; cipherV.at(i) = '$'; ++i;
    normalV.at(i) = 'p'; cipherV.at(i) = ']'; ++i;
    normalV.at(i) = 'q'; cipherV.at(i) = '}'; ++i;
    normalV.at(i) = 'r'; cipherV.at(i) = ';'; ++i;
    normalV.at(i) = 's'; cipherV.at(i) = ':'; ++i;
    normalV.at(i) = 't'; cipherV.at(i) = ','; ++i;
    normalV.at(i) = 'u'; cipherV.at(i) = '%'; ++i;
    normalV.at(i) = 'v'; cipherV.at(i) = '<'; ++i;
    normalV.at(i) = 'w'; cipherV.at(i) = '.'; ++i;
    normalV.at(i) = 'x'; cipherV.at(i) = '>'; ++i;
    normalV.at(i) = 'y'; cipherV.at(i) = '/'; ++i;
    normalV.at(i) = 'z'; cipherV.at(i) = '?'; ++i;

    // User inputs message
    do {
        cout << "Enter a secret message: ";
        getline(cin, toDec);
    } while (toDec.length() == 0);

    beenDec = toDec;

    //decodes user's message
    for (i = 0; i < setNum; ++i){
    if (toDec.at(0) == cipherV.at(i)) {
            beenDec.at(0) = normalV.at(i);
        }
    }

    //diplays decoded message
    cout << "Decrypted message: " << beenDec << endl;

    //command drive to stay open
    cin.get();
    cin.get();
    return 0;
}


Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags