update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
|
|
@ -1,20 +1,21 @@
|
|||
import std.stdio, std.traits;
|
||||
|
||||
pure S rot(S)(in S s, in int key) if (isSomeString!S) {
|
||||
auto res = s.dup;
|
||||
pure S rot(S)(in S s, in int key) pure /*nothrow*/
|
||||
if (isSomeString!S) {
|
||||
auto res = s.dup; // Not nothrow.
|
||||
|
||||
foreach (i, ref c; res) {
|
||||
foreach (immutable i, ref c; res) {
|
||||
if ('a' <= c && c <= 'z')
|
||||
c = ((c - 'a' + key) % 26 + 'a');
|
||||
else if ('A' <= c && c <= 'Z')
|
||||
c = ((c - 'A' + key) % 26 + 'A');
|
||||
}
|
||||
return cast(S) res;
|
||||
return res;
|
||||
}
|
||||
|
||||
void main() {
|
||||
int key = 3;
|
||||
auto txt = "The five boxing wizards jump quickly";
|
||||
enum key = 3;
|
||||
immutable txt = "The five boxing wizards jump quickly";
|
||||
writeln("Original: ", txt);
|
||||
writeln("Encrypted: ", txt.rot(key));
|
||||
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
|
||||
|
|
|
|||
17
Task/Caesar-cipher/D/caesar-cipher-3.d
Normal file
17
Task/Caesar-cipher/D/caesar-cipher-3.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio, std.ascii, std.string, std.algorithm;
|
||||
|
||||
string rot(in string s, in int key) pure /*nothrow*/ {
|
||||
auto uppr = uppercase.dup.representation;
|
||||
bringToFront(uppr[0 .. key], uppr[key .. $]);
|
||||
auto lowr = lowercase.dup.representation;
|
||||
bringToFront(lowr[0 .. key], lowr[key .. $]);
|
||||
return s.translate(makeTrans(letters, cast(char[])(uppr ~ lowr)));
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum key = 3;
|
||||
immutable txt = "The five boxing wizards jump quickly";
|
||||
writeln("Original: ", txt);
|
||||
writeln("Encrypted: ", txt.rot(key));
|
||||
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue