Vigenère cipher: Difference between revisions
Content added Content deleted
Recombinant (talk | contribs) m (→{{header|Zig}}: refactor parameter to use enum instead of bool) |
|||
Line 4,701: | Line 4,701: | ||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
{{works with|Zig|0.11. |
{{works with|Zig|0.11.0}} |
||
<syntaxhighlight lang="zig"> |
<syntaxhighlight lang="zig"> |
||
const std = @import("std"); |
|||
const Allocator = std.mem.Allocator; |
const Allocator = std.mem.Allocator; |
||
</syntaxhighlight> |
</syntaxhighlight><syntaxhighlight lang="zig"> |
||
const Vigenere = enum { |
|||
⚫ | |||
encode, |
|||
⚫ | |||
decode, |
|||
⚫ | |||
}; |
|||
⚫ | |||
</syntaxhighlight><syntaxhighlight lang="zig"> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
// ------------------------------------------ allocator |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// allocator |
|||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
||
defer { |
defer { |
||
Line 4,729: | Line 4,719: | ||
} |
} |
||
const allocator = gpa.allocator(); |
const allocator = gpa.allocator(); |
||
// --------------------------------------------- stdout |
|||
// |
|||
const stdout = std.io.getStdOut().writer(); |
const stdout = std.io.getStdOut().writer(); |
||
// ---------------------------------------------------- |
|||
// |
|||
const key = "VIGENERECIPHER"; |
const key = "VIGENERECIPHER"; |
||
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"; |
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"; |
||
const encoded = try vigenere(allocator, text, key, |
const encoded = try vigenere(allocator, text, key, .encode); |
||
defer allocator.free(encoded); |
defer allocator.free(encoded); |
||
try stdout.print("{s}\n", .{encoded}); |
try stdout.print("{s}\n", .{encoded}); |
||
const decoded = try vigenere(allocator, encoded, key, |
const decoded = try vigenere(allocator, encoded, key, .decode); |
||
defer allocator.free(decoded); |
defer allocator.free(decoded); |
||
try stdout.print("{s}\n", .{decoded}); |
try stdout.print("{s}\n", .{decoded}); |
||
} |
|||
}</syntaxhighlight> |
|||
</syntaxhighlight><syntaxhighlight lang="zig"> |
|||
{{out}} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
const n = switch (mode) { |
|||
⚫ | |||
.decode => 26 + c - k, |
|||
}; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
<pre>WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY |
<pre>WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY |
||
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre> |
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre> |