CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
21
Task/Caesar-cipher/D/caesar-cipher-1.d
Normal file
21
Task/Caesar-cipher/D/caesar-cipher-1.d
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import std.stdio, std.traits;
|
||||
|
||||
pure S rot(S)(in S s, in int key) if (isSomeString!S) {
|
||||
auto res = s.dup;
|
||||
|
||||
foreach (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;
|
||||
}
|
||||
|
||||
void main() {
|
||||
int key = 3;
|
||||
auto txt = "The five boxing wizards jump quickly";
|
||||
writeln("Original: ", txt);
|
||||
writeln("Encrypted: ", txt.rot(key));
|
||||
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
|
||||
}
|
||||
20
Task/Caesar-cipher/D/caesar-cipher-2.d
Normal file
20
Task/Caesar-cipher/D/caesar-cipher-2.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import std.stdio, std.ascii;
|
||||
|
||||
void inplaceRot(char[] txt, in int key) pure nothrow {
|
||||
foreach (ref c; txt) {
|
||||
if (isLower(c))
|
||||
c = (c - 'a' + key) % 26 + 'a';
|
||||
else if (isUpper(c))
|
||||
c = (c - 'A' + key) % 26 + 'A';
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum key = 3;
|
||||
auto txt = "The five boxing wizards jump quickly".dup;
|
||||
writeln("Original: ", txt);
|
||||
txt.inplaceRot(key);
|
||||
writeln("Encrypted: ", txt);
|
||||
txt.inplaceRot(26 - key);
|
||||
writeln("Decrypted: ", txt);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue