Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using std::string;
- using std::cout;
- using std::endl;
- const string chars = "abcdefghijklmnopqrstuvwxyz !?.";
- string encode(string input, const string key)
- {
- string output;
- int pos[2];
- for(int c = 0; c < input.length(); c++)
- {
- pos[0] = chars.find( tolower(input[c]) );
- pos[1] = chars.find( tolower(key[c]) );
- if(pos[0] != string::npos && pos[1] != string::npos)
- {
- output.append( chars.substr( ((pos[0] + pos[1]) % 30), 1) );
- }
- }
- return output;
- }
- string decode(string input, const string key)
- {
- string output;
- int pos[2];
- int p;
- for(int c = 0; c < input.length(); c++)
- {
- pos[0] = chars.find( tolower(input[c]) );
- pos[1] = chars.find( tolower(key[c]) );
- if(pos[0] != string::npos && pos[1] != string::npos)
- {
- p = (pos[0] - pos[1]);
- if(p < 0) p = 30+p;
- output.append( chars.substr( p, 1) );
- }
- }
- return output;
- }
- int main()
- {
- string message = "hello world";
- const string key = "thelemonade";
- string encoded;
- cout << "Original -> " << message << endl;
- encoded = encode(message, key);
- cout << "Encoded -> " << encoded << endl;
- cout << "Decoded -> " << decode(encoded, key) << endl;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement