'Using the Map function in order to assign letters to ASCll code and then using the encrpytions output the word the quick fox jumped over the lazy dog
#include <iostream>
#include <map>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
map<string, string> codes = {{"a", "a"},{"b", "b"},{"c", "c"},{"d", "d"},{"e","e"},{"f", "f"},{"g", "g"},
{"h", "h"}, {"i", "i"},{"j", "j"},{"k","k"},{"l","l"},{"m","m"},{"n","n"},
{"o", "o"},{"p","p"},{"q","q"},{"r","r"},{"s","s"},{"t","t"},{"u","u"},
{"v","v"},{"w","w"},{"x", "x"},{"y","y"},{"z","z"} };
cout << "Welcome user, the challenge today has you encrypting this sentence in ASCII format:" << endl;
cout << endl;
cout << "the quick fox jumped over the lazy dog" << endl;
cout << endl;
string encrypt;
while (encrypt != "Quit") {
cin >> encrypt;
cout << codes[encrypt] << endl;
}
return 0;
}
So right now, I'm having an Issue understanding how I can reverse the input so instead of when I type a and it displays a, it will display the ASCll code. I'm also having trouble trying how to basically set it to where once you have inputted all the ASCll codes and when you stop the program so it'll print out the response.
Solution 1:[1]
The code you have shown doesn't work, because you are passing the entire input std::string as-is to map::operator[], when you need to instead pass the individual characters of the std::string, eg:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<char, string> codes = { {'a', "a"}, {'b', "b"}, {'c', "c"}, {'d', "d"}, {'e', "e"}, {'f', "f"}, {'g', "g"},
{'h', "h"}, {'i', "i"}, {'j', "j"}, {'k', "k"}, {'l', "l"}, {'m', "m"}, {'n', "n"},
{'o', "o"}, {'p', "p"}, {'q', "q"}, {'r', "r"}, {'s', "s"}, {'t', "t"}, {'u', "u"},
{'v', "v"}, {'w', "w"}, {'x', "x"}, {'y', "y"}, {'z', "z"} };
cout << "Welcome user, the challenge today has you encrypting this sentence in ASCII format:" << endl;
cout << endl;
cout << "the quick fox jumped over the lazy dog" << endl;
cout << endl;
string input;
while (cin >> input && input != "Quit") {
for (char ch : input) {
cout << codes[ch] << endl;
}
}
return 0;
}
To reverse the process, you will have to scan through the "encrypted" string looking for &#<number>; sequences, and then for each sequence you can scan through the std::map to find a matching value, and if found then you will know the letter from its key, eg:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<char, string> codes = { {'a', "a"}, {'b', "b"}, {'c', "c"}, {'d', "d"}, {'e', "e"}, {'f', "f"}, {'g', "g"},
{'h', "h"}, {'i', "i"}, {'j', "j"}, {'k', "k"}, {'l', "l"}, {'m', "m"}, {'n', "n"},
{'o', "o"}, {'p', "p"}, {'q', "q"}, {'r', "r"}, {'s', "s"}, {'t', "t"}, {'u', "u"},
{'v', "v"}, {'w', "w"}, {'x', "x"}, {'y', "y"}, {'z', "z"} };
cout << "Welcome user, the challenge today has you encrypting this sentence in ASCII format:" << endl;
cout << endl;
cout << "the quick fox jumped over the lazy dog" << endl;
cout << endl;
string input;
while (cin >> input && input != "Quit") {
for (char ch : input) {
cout << codes[ch] << endl;
}
}
cout << "now, enter the codes back in" << endl;
cout << endl;
while (cin >> input && input != "Quit") {
for (auto &entry : codes) {
if (entry.second == input) {
cout << entry.first << endl;
break;
}
}
}
return 0;
}
However, as you can see, std::map is not well-suited for this kind of reverse lookup. You could setup a separate std::map for the code-to-letter lookup, eg:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<char, string> codes = { {'a', "a"}, {'b', "b"}, {'c', "c"}, {'d', "d"}, {'e', "e"}, {'f', "f"}, {'g', "g"},
{'h', "h"}, {'i', "i"}, {'j', "j"}, {'k', "k"}, {'l', "l"}, {'m', "m"}, {'n', "n"},
{'o', "o"}, {'p', "p"}, {'q', "q"}, {'r', "r"}, {'s', "s"}, {'t', "t"}, {'u', "u"},
{'v', "v"}, {'w', "w"}, {'x', "x"}, {'y', "y"}, {'z', "z"} };
map<char, string> letters = { {"a", 'a'}, {"b", 'b'}, {"c", 'c'}, {"d", 'd'}, {"e", 'e'}, {"f", 'f'}, {"g", 'g'},
{"h", 'h'}, {"i", 'i'}, {"j", 'j'}, {"k", 'k'}, {"l", 'l'}, {"m", 'm'}, {"n", 'n'},
{"o", 'o'}, {"p", 'p'}, {"q", 'q'}, {"r", 'r'}, {"s", 's'}, {"t", 't'}, {"u", 'u'},
{"v", 'v'}, {"w", 'w'}, {"x", 'x'}, {"y", 'y'}, {"z", 'z'} };
cout << "Welcome user, the challenge today has you encrypting this sentence in ASCII format:" << endl;
cout << endl;
cout << "the quick fox jumped over the lazy dog" << endl;
cout << endl;
string input;
while (cin >> input && input != "Quit") {
for (char ch : input) {
cout << codes[ch] << endl;
}
}
cout << "now, enter the codes back in" << endl;
cout << endl;
while (cin >> input && input != "Quit") {
cout << letters[input] << endl;
}
return 0;
}
However, you don't actually need the maps at all. Simply type-cast a char into an integer to get its numeric code. And parse an entity code to extract its integer and then cast that back to a char, eg:
#include <iostream>
#include <map>
using namespace std;
int main() {
cout << "Welcome user, the challenge today has you encrypting this sentence in ASCII format:" << endl;
cout << endl;
cout << "the quick fox jumped over the lazy dog" << endl;
cout << endl;
string input;
while (cin >> input && input != "Quit") {
for (char ch : input) {
cout << "&#" << static_cast<int>(ch) << ";" << endl;
}
}
cout << "now, enter the codes back in" << endl;
cout << endl;
while (cin >> input && input != "Quit") {
string s = input.substr(2, input.size()-3);
int i = stoi(s);
cout << static_cast<char>(i) << endl;
}
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
