RosettaCodeData/Task/Vigen-re-cipher/Zig/vigen-re-cipher-3.zig

23 lines
860 B
Zig
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
pub fn main() anyerror!void {
2023-09-16 17:28:03 -07:00
// ------------------------------------------ allocator
2023-07-01 11:58:00 -04:00
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const ok = gpa.deinit();
std.debug.assert(ok == .ok);
}
const allocator = gpa.allocator();
2023-09-16 17:28:03 -07:00
// --------------------------------------------- stdout
2023-07-01 11:58:00 -04:00
const stdout = std.io.getStdOut().writer();
2023-09-16 17:28:03 -07:00
// ----------------------------------------------------
2023-07-01 11:58:00 -04:00
const key = "VIGENERECIPHER";
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
2023-09-16 17:28:03 -07:00
const encoded = try vigenere(allocator, text, key, .encode);
2023-07-01 11:58:00 -04:00
defer allocator.free(encoded);
2023-07-01 19:04:33 -04:00
try stdout.print("{s}\n", .{encoded});
2023-07-01 11:58:00 -04:00
2023-09-16 17:28:03 -07:00
const decoded = try vigenere(allocator, encoded, key, .decode);
2023-07-01 11:58:00 -04:00
defer allocator.free(decoded);
2023-07-01 19:04:33 -04:00
try stdout.print("{s}\n", .{decoded});
2023-07-01 11:58:00 -04:00
}