All pastes #791737 Raw Edit

Something

public text v1 · immutable
·published 2007-11-21 01:32 UTC
rendered paste body
/************************************************************************************************************************************************************************
* Encoder/Decoder
* Charles Wood-Henderson
* To encrypt strings and decrypt them.
* Acknowledgements: none
************************************************************************************************************************************************************************/

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

/***********************************************************************************************************************************************************************
* cipher()
* none
* class
* To encode/decode strings.
************************************************************************************************************************************************************************/
class cipher {
private:
	string line;
	int shift_key;
public:
	cipher() : shift_key(0)
	{    }
	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() {
	bool uppercase = false;
	cout << "Please enter a string that you would like to encode: ";
	cin >> line;
	do {
	cout << "What integer cipher shift key will you use? (1-20) ";
	cin >> shift_key;
	int length = line.length();
	for(int j=0; j<length; j++) {
		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; // 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;
		}
	}
	cout << "Your encoded string is:\n";
	for(int j=0; j<length; j++) {
		cout << line.at(j); // printing the encoded string
	}
	cout << endl;
}

void cipher::decode() {
	bool uppercase = false;
	cout << "Please enter a string that you would like to decode: ";
	cin >> line;
	cout << "What integer cipher shift key will you use? (1-20) ";
	cin >> shift_key;

	int length = line.length();

	for(int j=0; j<length; j++) {
		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;
		}
	}
	cout << "Your decoded string is:\n";
	for(int j=0; j<length; j++) {
		cout << line.at(j); // printing the decoded string to the screen
	}
	cout << endl;
}