diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-02-09 18:36:57 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-02-09 18:36:57 -0500 |
commit | 55ad7c32f2d534b1fbd438204d21738f958c51a5 (patch) | |
tree | 16be4b53193105a759b3eec25be5e664d41c428d /exercises/06_strings.zig | |
parent | cf0920de31e9b5f3c5ba6de19a1b4c8d0c58b907 (diff) | |
download | ziglings-55ad7c32f2d534b1fbd438204d21738f958c51a5.tar.gz ziglings-55ad7c32f2d534b1fbd438204d21738f958c51a5.tar.bz2 ziglings-55ad7c32f2d534b1fbd438204d21738f958c51a5.tar.xz ziglings-55ad7c32f2d534b1fbd438204d21738f958c51a5.zip |
Moved exercises to exercises because exercises
Diffstat (limited to 'exercises/06_strings.zig')
-rw-r--r-- | exercises/06_strings.zig | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/exercises/06_strings.zig b/exercises/06_strings.zig new file mode 100644 index 0000000..2430884 --- /dev/null +++ b/exercises/06_strings.zig @@ -0,0 +1,48 @@ +// +// Now that we've learned about arrays, we can talk about strings. +// +// We've already seen Zig string literals: "Hello world.\n" +// +// Zig stores strings as arrays of bytes. +// +// const foo = "Hello"; +// +// Is the same as: +// +// const foo = [_]u8{ 'H', 'e', 'l', 'l', 'o' }; +// +const std = @import("std"); + +pub fn main() void { + const ziggy = "stardust"; + + // (Problem 1) + // Use array square bracket syntax to get the letter 'd' from + // the string "stardust" above. + const d: u8 = ziggy[???]; + + // (Problem 2) + // Use the array repeat '**' operator to make "ha ha ha". + const laugh = "ha " ???; + + // (Problem 3) + // Use the array concatenation '++' operator to make "Major Tom". + // (You'll need to add a space as well!) + const major = "Major"; + const tom = "Tom"; + const major_tom = major ??? tom; + + // That's all the problems. Let's see our results: + std.debug.print("d={u} {}{}\n",.{d, laugh, major_tom}); + // + // Keen eyes will notice that we've put a 'u' inside the '{}' + // placeholder in the format string above. This tells the + // print() function to format the values as a UTF-8 character. + // If we didn't do this, we'd see '100', which is the decimal + // number corresponding with the 'd' character in UTF-8. + // + // While we're on this subject, 'c' (ASCII encoded character) + // would work in place for 'u' because the first 128 characters + // of UTF-8 are the same as ASCII! + // +} |