TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
a1z26_cipher.cpp File Reference

Implementation of the A1Z26 cipher More...

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
Include dependency graph for a1z26_cipher.cpp:

Go to the source code of this file.

Namespaces

namespace  ciphers
 Algorithms for encryption and decryption.
 
namespace  a1z26
 Functions for A1Z26 encryption and decryption implementation.
 

Functions

std::string ciphers::a1z26::encrypt (std::string text)
 a1z26 encryption implementation
 
std::string ciphers::a1z26::decrypt (const std::string &text, bool bReturnUppercase=false)
 a1z26 decryption implementation
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Variables

std::map< uint8_t, char > ciphers::a1z26::a1z26_decrypt_map
 
std::map< char, uint8_t > ciphers::a1z26::a1z26_encrypt_map
 

Detailed Description

Implementation of the A1Z26 cipher

The A1Z26 cipher is a simple substiution cipher where each letter is replaced by the number of the order they're in. For example, A corresponds to 1, B = 2, C = 3, etc.

Author
Focusucof

Definition in file a1z26_cipher.cpp.

Function Documentation

◆ decrypt()

std::string ciphers::a1z26::decrypt ( const std::string & text,
bool bReturnUppercase = false )

a1z26 decryption implementation

Parameters
textis the encrypted text input
bReturnUppercaseis if the decoded string should be in uppercase or not
Returns
the decrypted string in all uppercase or all lowercase

Definition at line 78 of file a1z26_cipher.cpp.

78 {
79 std::string result;
80
81 // split words seperated by spaces into a vector array
82 std::vector<std::string> word_array;
83 std::stringstream sstream(text);
84 std::string word;
85 while (sstream >> word) {
86 word_array.push_back(word);
87 }
88
89 for (auto& i : word_array) {
90 std::replace(i.begin(), i.end(), '-', ' ');
91 std::vector<std::string> text_array;
92
93 std::stringstream ss(i);
94 std::string res_text;
95 while (ss >> res_text) {
96 text_array.push_back(res_text);
97 }
98
99 for (auto& i : text_array) {
100 result += a1z26_decrypt_map[stoi(i)];
101 }
102
103 result += ' ';
104 }
105 result.pop_back(); // remove any leading whitespace
106
107 if (bReturnUppercase) {
108 std::transform(result.begin(), result.end(), result.begin(), ::toupper);
109 }
110 return result;
111}
uint64_t result(uint64_t n)

◆ encrypt()

std::string ciphers::a1z26::encrypt ( std::string text)

a1z26 encryption implementation

Parameters
textis the plaintext input
Returns
encoded string with dashes to seperate letters

Definition at line 51 of file a1z26_cipher.cpp.

51 {
52 std::string result;
53 std::transform(text.begin(), text.end(), text.begin(),
54 ::tolower); // convert string to lowercase
55 std::replace(text.begin(), text.end(), ':', ' ');
56 for (char letter : text) {
57 if (letter != ' ') {
58 result += std::to_string(
59 a1z26_encrypt_map[letter]); // convert int to string and append
60 // to result
61 result += "-"; // space out each set of numbers with spaces
62 } else {
63 result.pop_back();
64 result += ' ';
65 }
66 }
67 result.pop_back(); // remove leading dash
68 return result;
69}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 159 of file a1z26_cipher.cpp.

159 {
160 test(); // run self-test implementations
161 return 0;
162}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 120 of file a1z26_cipher.cpp.

120 {
121 // 1st test
122 std::string input = "Hello World";
123 std::string expected = "8-5-12-12-15 23-15-18-12-4";
124 std::string output = ciphers::a1z26::encrypt(input);
125
126 std::cout << "Input: " << input << std::endl;
127 std::cout << "Expected: " << expected << std::endl;
128 std::cout << "Output: " << output << std::endl;
129 assert(output == expected);
130 std::cout << "TEST PASSED";
131
132 // 2nd test
133 input = "12-15-23-5-18-3-1-19-5";
134 expected = "lowercase";
135 output = ciphers::a1z26::decrypt(input);
136
137 std::cout << "Input: " << input << std::endl;
138 std::cout << "Expected: " << expected << std::endl;
139 std::cout << "Output: " << output << std::endl;
140 assert(output == expected);
141 std::cout << "TEST PASSED";
142
143 // 3rd test
144 input = "21-16-16-5-18-3-1-19-5";
145 expected = "UPPERCASE";
146 output = ciphers::a1z26::decrypt(input, true);
147
148 std::cout << "Input: " << input << std::endl;
149 std::cout << "Expected: " << expected << std::endl;
150 std::cout << "Output: " << output << std::endl;
151 assert(output == expected);
152 std::cout << "TEST PASSED";
153}
std::string decrypt(const std::string &text, bool bReturnUppercase=false)
a1z26 decryption implementation
std::string encrypt(std::string text)
a1z26 encryption implementation

Variable Documentation

◆ a1z26_decrypt_map

std::map<uint8_t, char> ciphers::a1z26::a1z26_decrypt_map
Initial value:
= {
{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}, {6, 'f'}, {7, 'g'},
{8, 'h'}, {9, 'i'}, {10, 'j'}, {11, 'k'}, {12, 'l'}, {13, 'm'}, {14, 'n'},
{15, 'o'}, {16, 'p'}, {17, 'q'}, {18, 'r'}, {19, 's'}, {20, 't'}, {21, 'u'},
{22, 'v'}, {23, 'w'}, {24, 'x'}, {25, 'y'}, {26, 'z'},
}

Definition at line 33 of file a1z26_cipher.cpp.

33 {
34 {1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}, {6, 'f'}, {7, 'g'},
35 {8, 'h'}, {9, 'i'}, {10, 'j'}, {11, 'k'}, {12, 'l'}, {13, 'm'}, {14, 'n'},
36 {15, 'o'}, {16, 'p'}, {17, 'q'}, {18, 'r'}, {19, 's'}, {20, 't'}, {21, 'u'},
37 {22, 'v'}, {23, 'w'}, {24, 'x'}, {25, 'y'}, {26, 'z'},
38};

◆ a1z26_encrypt_map

std::map<char, uint8_t> ciphers::a1z26::a1z26_encrypt_map
Initial value:
= {
{'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, {'f', 6}, {'g', 7},
{'h', 8}, {'i', 9}, {'j', 10}, {'k', 11}, {'l', 12}, {'m', 13}, {'n', 14},
{'o', 15}, {'p', 16}, {'q', 17}, {'r', 18}, {'s', 19}, {'t', 20}, {'u', 21},
{'v', 22}, {'w', 23}, {'x', 24}, {'y', 25}, {'z', 26}}

Definition at line 40 of file a1z26_cipher.cpp.

40 {
41 {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, {'f', 6}, {'g', 7},
42 {'h', 8}, {'i', 9}, {'j', 10}, {'k', 11}, {'l', 12}, {'m', 13}, {'n', 14},
43 {'o', 15}, {'p', 16}, {'q', 17}, {'r', 18}, {'s', 19}, {'t', 20}, {'u', 21},
44 {'v', 22}, {'w', 23}, {'x', 24}, {'y', 25}, {'z', 26}};