-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalt-split.cpp
More file actions
57 lines (48 loc) · 1.35 KB
/
Copy pathalt-split.cpp
File metadata and controls
57 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
std::string encrypt(std::string text, int n) {
if (text.empty() || n < 1) return text;
while(n > 0)
std::string odd = "";
std::string even = "";
for(int i = 0; i < text.length(); i++){
(i % 2 == 0) ? even += text[i] : odd += text[i];
}
text = odd + even;
n--;
}
return text;
}
/** Inverse of the encryption function
* -Now we start with non-empty string objects for odd & even as opposed to
* to the empty ones in the encrypt function
*
* **/
std::string decrypt(std::string encryptedText, int n) {
if (encryptedText.empty() || n < 1) {
return encryptedText;
}
int textLength, textHalf;
std::string odd, even;
while (n > 0) {
textLength = encryptedText.length();
textHalf = textLength / 2;
odd = encryptedText.substr(0, textHalf);
even = encryptedText.substr(textHalf);
encryptedText = "";
for (int i = 0; i < textHalf; i++) {
encryptedText += even[i];
encryptedText += odd[i];
}
if (textLength % 2 != 0) {
encryptedText += even[textHalf];
}
n--;
}
return encryptedText;
}
int main () {
encrypt("This is a test!", 0);
std::cout << decrypt("hsi etTi sats!", 1) << std::endl;
return 0;
}