diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
commit | 6ad9774189fbd64b2f2c9519f4513ab34b0c3809 (patch) | |
tree | d6c90700131d5b28e898881f13e2a05612e4703f /exercises/053_slices2.zig | |
parent | be36352572ddb18218e1830e49316c259dea5e8c (diff) | |
download | ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.gz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.bz2 ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.xz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.zip |
"999 is enough for anybody" triple-zero padding (#18)
When I hit 999 exercises, I will finally have reached the ultimate
state of soteriological release and no more exercises will be needed.
The cycle will be complete. All that will be left is perfect quietude,
freedom, and highest happiness.
Diffstat (limited to 'exercises/053_slices2.zig')
-rw-r--r-- | exercises/053_slices2.zig | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/exercises/053_slices2.zig b/exercises/053_slices2.zig new file mode 100644 index 0000000..2456d86 --- /dev/null +++ b/exercises/053_slices2.zig @@ -0,0 +1,35 @@ +// +// You are perhaps tempted to try slices on strings? They're arrays of +// u8 characters after all, right? Slices on strings work great. +// There's just one catch: don't forget that Zig string literals are +// immutable (const) values. So we need to change the type of slice +// from: +// +// var foo: []u8 = "foobar"[0..3]; +// +// to: +// +// var foo: []const u8 = "foobar"[0..3]; +// +// See if you can fix this Zero Wing-inspired phrase descrambler: +const std = @import("std"); + +pub fn main() void { + const scrambled = "great base for all your justice are belong to us"; + + const base1: []u8 = scrambled[15..23]; + const base2: []u8 = scrambled[6..10]; + const base3: []u8 = scrambled[32..]; + printPhrase(base1, base2, base3); + + const justice1: []u8 = scrambled[11..14]; + const justice2: []u8 = scrambled[0..5]; + const justice3: []u8 = scrambled[24..31]; + printPhrase(justice1, justice2, justice3); + + std.debug.print("\n", .{}); +} + +fn printPhrase(part1: []u8, part2: []u8, part3: []u8) void { + std.debug.print("'{s} {s} {s}.' ", .{part1, part2, part3}); +} |