diff --git a/cpp/decrypt.cpp b/cpp/decrypt.cpp new file mode 100644 index 0000000..92a0d84 --- /dev/null +++ b/cpp/decrypt.cpp @@ -0,0 +1,37 @@ +// ============================================================================== +// +// Use: +// decrypt("HWe o!lordll", 4) +// => "Hello World!" +// +// ============================================================================== + +#include + +std::string decrypt(std::string s, int n) { + if(n<2) return s; + + std::string res = s; + + int a = -1, b = 2 * (n - 1) - 1, c = a, idx = 0; + + for(int i=0; i0 && b>0) c = c == a ? b : a; + else c = a > b ? a : b; + + j += c + 1; + } + + a += 2; + b -= 2; + c = a; + } + + return res; +} \ No newline at end of file diff --git a/cpp/encrypt.cpp b/cpp/encrypt.cpp new file mode 100644 index 0000000..2a27747 --- /dev/null +++ b/cpp/encrypt.cpp @@ -0,0 +1,36 @@ +// ============================================================================== +// +// Use: +// encrypt("Hello World", 4) +// => "HWe o!lordll" +// +// ============================================================================== + +#include + +std::string encrypt(std::string s, int n) { + if(n<2) return s; + + std::string res; + + int a = -1, b = 2 * (n - 1) - 1, c; + + for(int i=0; i0 && b>0) c = (c == a ? b : a); + else c = a > b ? a : b; + + j += c + 1; + } + + a += 2; + b -= 2; + c = a; + } + + return res; +} \ No newline at end of file