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;
}
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;
}
Review
Review
24/7 Homework Help
Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!
Similar Content
Related Tags
1984
by George Orwell
The Catcher in the Rye
by J. D. Salinger
What Happened
by Hillary Clinton
Narrative of the Life of Frederick Douglass
by Frederick Douglass
The Great Gatsby
by Francis Scott Key Fitzgerald
Hidden Figures
by Margot Lee Shetterly
The Girl With The Dragon Tattoo
by Stieg Larsson
Shattered - Inside Hillary Clintons Doomed Campaign
by Amie Parnes and Jonathan Allen
Fear - Trump in the White House
by Bob Woodward