1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
/*
Program: ANewAlphabet.cpp
Author: Piggy
Program Information:
This program will replace normal letters of the alphabet with
new characters (can range from 1-6 chars per letter).
Algorithm Steps:
1. Ask user to input a string.
2. Convert string to lowercase so it is NOT caSe-SeNSiTiVe.
3. call checkDict() function which will compare all of the characters
in the string to those in the dictionary. Then will convert them to
their translated form and return those.
- Set up contents of unordered map in checkDict() function.
4. Output newly translated info.
5. Add assert() test cases (at least 3)
6. Review instructions to make sure I didn't forget anything.
- Also comment out cout << "Please input a string: "; etc..
*/
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm> // for transform()
#include <assert.h>
// Will only be using this for map related stuff because
// I'm new to it and this makes it easier to learn.
using namespace std;
void test();
std::string transToLower(std::string uIn);
std::string checkMap(std::string userInput);
int main() {
// Declaring variables
std::string uInput, lowerUInput, result;
// Automatically testing program:
test();
// Asks for user input and stores information into uInput
std::cout << "Please input a string: ";
std::getline(std::cin, uInput);
// Makes user input all lowercase so checkDict() works properly.
uInput = transToLower(uInput);
//std::cout << "[Debug] uInput = " << uInput << "\n";
// Checks unordered_map for each character in string
// then returns translated values.
result = checkMap(uInput);
// Outputs result
std::cout << result << "\n";
return 0;
}
// Converts user input string to lowercase.
std::string transToLower(std::string uIn) {
std::transform(uIn.begin(), uIn.end(), uIn.begin(), ::tolower);
return uIn;
}
// Checks dictionary for each character in string then returns translated values.
std::string checkMap(std::string userInput) {
// Declares an unordered map of type <string, string> called stringMap.
// AKA <key in map, mapped value>. Example: {"a", "@"}
unordered_map<string, string> stringMap;
const typedef unordered_map<string, string> stringMap;
// unordered_map
stringMap { {"a", "@"}, {"b", "8"}, {"c", "("}, {"d", "|)"}, {"e", "3"}
{"f", "#"}, {"g", "6"}, {"h", "[-]"}, {"i", "|"},
{"j", "_|"}, {"k", "|<"}, {"l", "1"}, {"m", "[]\\/[]"},
{"n", "[]\\[]"}, {"o", "0"}, {"p", "|D"}, {"q", "(,)"},
{"r", "|Z"}, {"s", "$"}, {"t", "']['"}, {"u", "|_|"},
{"v", "\\/"}, {"w", "\\/\\/"}, {"x", "}{"}, {"y", "`/"},
{"z", "2"} };
// Need to update the return once done
return "0";
}
void test() {
assert(transToLower("THIS is a TEST") == "this is a test");
//assert();
//assert();
//assert();
std::cout << "All test cases passed! \n";
}
|