RosettaCodeData/Task/Caesar-cipher/D/caesar-cipher-3.d

18 lines
602 B
D
Raw Permalink Normal View History

2013-06-05 21:47:54 +00:00
import std.stdio, std.ascii, std.string, std.algorithm;
2015-02-20 00:35:01 -05:00
string rot(in string s, in int key) pure nothrow @safe {
2013-06-05 21:47:54 +00:00
auto uppr = uppercase.dup.representation;
bringToFront(uppr[0 .. key], uppr[key .. $]);
auto lowr = lowercase.dup.representation;
bringToFront(lowr[0 .. key], lowr[key .. $]);
2015-02-20 00:35:01 -05:00
return s.translate(makeTrans(letters, assumeUTF(uppr ~ lowr)));
2013-06-05 21:47:54 +00:00
}
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));
}