053_slices2.zig 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // You are perhaps tempted to try slices on strings? They're arrays of
  3. // u8 characters after all, right? Slices on strings work great.
  4. // There's just one catch: don't forget that Zig string literals are
  5. // immutable (const) values. So we need to change the type of slice
  6. // from:
  7. //
  8. // var foo: []u8 = "foobar"[0..3];
  9. //
  10. // to:
  11. //
  12. // var foo: []const u8 = "foobar"[0..3];
  13. //
  14. // See if you can fix this Zero Wing-inspired phrase descrambler:
  15. const std = @import("std");
  16. pub fn main() void{
  17. const scrambled = "great base for all your justice are belong to us";
  18. const base1: []const u8 = scrambled[15..23];
  19. const base2: []const u8 = scrambled[6..10];
  20. const base3: []const u8 = scrambled[32..];
  21. printPhrase(base1, base2, base3);
  22. const justice1: []const u8 = scrambled[11..14];
  23. const justice2: []const u8 = scrambled[0..5];
  24. const justice3: []const u8 = scrambled[24..31];
  25. printPhrase(justice1, justice2, justice3);
  26. std.debug.print("\n", .{});
  27. }
  28. fn printPhrase(part1: []const u8, part2: []const u8, part3: []const u8) void{
  29. std.debug.print("'{s} {s} {s}.' ", .{ part1, part2, part3 });
  30. }