Caesar cipher: Difference between revisions

Updated D code
(Updated D code)
Line 176:
 
=={{header|D}}==
<lang d>import std.stdio, std.traits, std.ctype, std.convascii;
 
Sstring rot(S)(Simmutable string stxt, in int key) ifpure (isSomeString!S)nothrow {
dcharchar[] r = new dchar[s.length];
 
foreach (i, dchar c; stxt) {
if (islowerisLower(c))
cr ~= ((c - 'a' + key) % 26 + 'a');
else if (isupperisUpper(c))
cr ~= ((c - 'A' + key) % 26 + 'A');
r[i] = c; else
} r ~= c;
 
return to!S(r);
}
 
Line 203 ⟶ 204:
Decrypted: The five boxing wizards jump quickly</pre>
Simpler in-place version (same output):
<lang d>import std.stdio, std.ctypeascii;
 
void inplaceRot(char[] txt, in int key) pure nothrow {
foreach (ref c; txt) {
if (islowerisLower(c))
c = cast(char)((c - 'a' + key) % 26 + 'a');
else if (isupperisUpper(c))
c = cast(char)((c - 'A' + key) % 26 + 'A');
}
}
Anonymous user