/************************************************************************************************************************************************************************ * Encoder/Decoder * Charles Wood-Henderson * To encrypt strings and decrypt them. * Acknowledgements: none ************************************************************************************************************************************************************************/ #include #include using namespace std; /*********************************************************************************************************************************************************************** * cipher() * none * class * To encode/decode strings. ************************************************************************************************************************************************************************/ class cipher { private: string line; int shift_key; bool encoder; public: cipher() : shift_key(0) { } void get_shift_key(); void print(); void encode(); void decode(); }; /*********************************************************************************************************************************************************************** * main() * none * int * Required to run the program. ***********************************************************************************************************************************************************************/ int main() { cipher cipher1; // creating a class string do_what; // creating a string to see what the user wants to do bool repeat = true; while(repeat == true) { cout << "Welcome to the Caesar Encoder/Decoder!\n" << "Would you like to encode or decode or quit? "; cin >> do_what; if(do_what == "encode") { cipher1.encode(); // seeing what the user wants to do } else if(do_what == "decode") { cipher1.decode(); // seeing what the user wants to do } else if(do_what == "quit") { // seeing what the user wants to do return 0; } } } void cipher::encode() { encoder = true; bool uppercase = false; cout << "Please enter a string that you would like to encode: \n(Please end your string with a ~ symbol and then press the enter key)\n"; getline(cin, line, '~'); get_shift_key(); int length = line.length(); for(int j = 0; j < length; j++) { if(line[j] >= 'A' && line[j] <= 'Z' || line[j] >= 'a' && line[j] <= 'z') { if(line[j] < 'a') uppercase = true; else uppercase = false; line.at(j) = line.at(j) + shift_key; // shifting the letter how many places the user asked for if(line.at(j) > 'z' && uppercase == false) { int k = line.at(j) - 'z'; // checking if the places shifted goes over z when its lowercase line.at(j) = 'a' + k - 1; } if(line.at(j) > 'Z' && uppercase == true) { int k = line.at(j) - 'Z'; // checking if the places shifted goes over Z when its uppercase line.at(j) = 'A' + k - 1; } } } print(); cout << endl; } void cipher::decode() { encoder = false; bool uppercase = false; cout << "Please enter a string that you would like to encode: \n(Please end your string with a ~ symbol and then press the enter key)\n"; getline(cin, line, '~'); get_shift_key(); int length = line.length(); for(int j = 0; j < length; j++) { if(line[j] >= 'A' && line[j] <= 'Z' || line[j] >= 'a' && line[j] <= 'z') { if(line.at(j) < 'a') uppercase = true; else // checking if the letter is uppercase or lowercase uppercase = false; line.at(j) = line.at(j) - shift_key; if(line.at(j) < 'a' && uppercase == false) { int k = 'a' - line.at(j); // checking that the places needed to go backwards goes less than a in lowercase line.at(j) = 'z' - k + 1; } if(line.at(j) < 'A' && uppercase == true) { int k = 'A' - line.at(j);// checking that the places needed to go backwards goes less than a in uppercase line.at(j) = 'Z' - k + 1; } } } print(); } void cipher::print() { if(encoder == true) cout << "Your encoded string is: " << line << endl; else cout << "Your decoded string is: " << line << endl; } void cipher::get_shift_key() { cout << "What integer cipher shift key will you use? (1-20) "; cin >> shift_key; }