Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Vigen-re-cipher/Zig/vigen-re-cipher-1.zig
Normal file
2
Task/Vigen-re-cipher/Zig/vigen-re-cipher-1.zig
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
16
Task/Vigen-re-cipher/Zig/vigen-re-cipher-2.zig
Normal file
16
Task/Vigen-re-cipher/Zig/vigen-re-cipher-2.zig
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/// Caller owns the returned slice memory.
|
||||
fn vigenere(allocator: Allocator, text: []const u8, key: []const u8, encrypt: bool) Allocator.Error![]u8 {
|
||||
var dynamic_string = std.ArrayList(u8).init(allocator);
|
||||
var key_index: usize = 0;
|
||||
for (text) |letter| {
|
||||
const c = if (std.ascii.isLower(letter)) std.ascii.toUpper(letter) else letter;
|
||||
if (std.ascii.isUpper(c)) {
|
||||
const k = key[key_index];
|
||||
const n = if (encrypt) ((c - 'A') + (k - 'A')) else 26 + c - k;
|
||||
try dynamic_string.append(n % 26 + 'A'); // A-Z
|
||||
key_index += 1;
|
||||
key_index %= key.len;
|
||||
}
|
||||
}
|
||||
return dynamic_string.toOwnedSlice();
|
||||
}
|
||||
22
Task/Vigen-re-cipher/Zig/vigen-re-cipher-3.zig
Normal file
22
Task/Vigen-re-cipher/Zig/vigen-re-cipher-3.zig
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
pub fn main() anyerror!void {
|
||||
// allocator
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer {
|
||||
const ok = gpa.deinit();
|
||||
std.debug.assert(ok == .ok);
|
||||
}
|
||||
const allocator = gpa.allocator();
|
||||
//
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
//
|
||||
const key = "VIGENERECIPHER";
|
||||
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
|
||||
const encoded = try vigenere(allocator, text, key, true);
|
||||
defer allocator.free(encoded);
|
||||
_ = try stdout.print("{s}\n", .{encoded});
|
||||
|
||||
const decoded = try vigenere(allocator, encoded, key, false);
|
||||
defer allocator.free(decoded);
|
||||
_ = try stdout.print("{s}\n", .{decoded});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue