exercism/zig/rotational-cipher/rotational_cipher.zig

16 lines
443 B
Zig

const std = @import("std");
const mem = std.mem;
pub fn rotate(allocator: mem.Allocator, text: []const u8, shiftKey: u5) mem.Allocator.Error![]u8 {
const result = try allocator.alloc(u8, text.len);
for (text, 0..) |c, i| {
result[i] = switch(c) {
'a'...'z' => (c - 'a' + shiftKey) % 26 + 'a',
'A'...'Z' => (c - 'A' + shiftKey) % 26 + 'A',
else => c,
};
}
return result;
}