Caesar cipher: Difference between revisions
Content added Content deleted
(Updated D code) |
|||
Line 176: | Line 176: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std. |
<lang d>import std.stdio, std.ascii; |
||
string rot(immutable string txt, in int key) pure nothrow { |
|||
char[] r; |
|||
foreach ( |
foreach (c; txt) |
||
if ( |
if (isLower(c)) |
||
r ~= (c - 'a' + key) % 26 + 'a'; |
|||
else if ( |
else if (isUpper(c)) |
||
r ~= (c - 'A' + key) % 26 + 'A'; |
|||
else |
|||
r ~= c; |
|||
return |
return r; |
||
} |
} |
||
Line 203: | Line 204: | ||
Decrypted: The five boxing wizards jump quickly</pre> |
Decrypted: The five boxing wizards jump quickly</pre> |
||
Simpler in-place version (same output): |
Simpler in-place version (same output): |
||
<lang d>import std.stdio, std. |
<lang d>import std.stdio, std.ascii; |
||
void inplaceRot(char[] txt, in int key) { |
void inplaceRot(char[] txt, in int key) pure nothrow { |
||
foreach (ref c; txt) { |
foreach (ref c; txt) { |
||
if ( |
if (isLower(c)) |
||
c = |
c = (c - 'a' + key) % 26 + 'a'; |
||
else if ( |
else if (isUpper(c)) |
||
c = |
c = (c - 'A' + key) % 26 + 'A'; |
||
} |
} |
||
} |
} |