exercism/zig/rotational-cipher/rotational_cipher.zig

17 lines
443 B
Zig
Raw Normal View History

2024-09-14 11:24:39 -04:00
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;
}