Rot-13: Difference between revisions

173 bytes added ,  12 years ago
Updated 2 D entries
(Updated 2 D entries)
Line 491:
 
=={{header|D}}==
===Using Standard Functions===
{{works with|D|2}}
<lang d>import std.stdio, std.string;
import std.ascii: letters, U = uppercase, L = lowercase;
import std.string: maketrans, translate;
 
immutable r13 = maketrans(letters,
lowercase//U[13 .. $] ~ lowercaseU[0 .. 13]); ~
U[13 .. U.length] ~ U[0 .. 13] ~
L[13 .. L.length] ~ L[0 .. 13]);
 
void main() {
writeflnwriteln("This is the 1st test!".translate(r13, null));
}</lang>
Output:
<pre>The Quick Brown Fox Jumps Over The Lazy Dog!</pre>
===Imperative Implementation===
<lang d>import std.stdio, std.traits, std.ascii, std.range, std.conv;
S rot13(S)(in S s) if (isSomeString!S) {
return rot(s, 13);
 
S rot(S)(in S s, in int key) if (isSomeString!S) {
auto r = new dchar[s.walkLength];
 
foreach (i, const dchar c; s) {
if (std.asciic.isLower(c))
cr[i] = ((c - 'a' + key) % 26 + 'a');
else if (std.asciic.isUpper(c))
cr[i] = ((c - 'A' + key) % 26 + 'A');
r[i] = c; else
r[i] = c;
}
 
return to!S(r);
 
S rot13(S)(in S s) if (isSomeString!S) {
return rot(s, 13);
}
 
void main() {
writeln("Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt!".rot13().writeln();
}</lang>
Output:
<pre>The Quick Brown Fox Jumps Over The Lazy Dog!</pre>
{{works with|D|1}}
<lang d>import std.stdio, std.string;
void main() {
auto r13 = letters.maketrans(uppercase[13..$] ~ uppercase[0..13] ~
lowercase[13..$] ~ lowercase[0..13]);
writefln("This is the 1st test!".translate(r13, null));
}</lang>