aboutsummaryrefslogtreecommitdiff
path: root/06_strings.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-02-07 11:06:51 -0500
committerDave Gauer <dave@ratfactor.com>2021-02-07 11:06:51 -0500
commitadf5ddb27df7f5a22b0b7d3321dfc8bca1e7937a (patch)
treea25511c3bb20069f1d6123366573c82c5745338b /06_strings.zig
parent507355ec3b1066c707e19816b86ac1fb56fc0385 (diff)
downloadziglings-adf5ddb27df7f5a22b0b7d3321dfc8bca1e7937a.tar.gz
ziglings-adf5ddb27df7f5a22b0b7d3321dfc8bca1e7937a.tar.bz2
ziglings-adf5ddb27df7f5a22b0b7d3321dfc8bca1e7937a.tar.xz
ziglings-adf5ddb27df7f5a22b0b7d3321dfc8bca1e7937a.zip
Consistent instructions and examples
I started off with "hints" that required the poor student to piece together the information from incomplete bits. A complete example is like a picture that is worth 1000 words and far clearer.
Diffstat (limited to '06_strings.zig')
-rw-r--r--06_strings.zig26
1 files changed, 17 insertions, 9 deletions
diff --git a/06_strings.zig b/06_strings.zig
index cac40e0..2430884 100644
--- a/06_strings.zig
+++ b/06_strings.zig
@@ -3,38 +3,46 @@
//
// We've already seen Zig string literals: "Hello world.\n"
//
-// Like the C language, Zig stores strings as arrays of bytes
-// encoded as UTF-8 characters terminated with a null value.
-// For now, just focus on the fact that strings are arrays of
-// characters!
+// 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});
- // Going deeper:
+ //
// Keen eyes will notice that we've put a 'u' inside the '{}'
// placeholder in the format string above. This tells the
- // print() function (which uses std.fmt.format() function) to
- // print out a UTF-8 character. Otherwise we'd see '100', which
- // is the decimal number corresponding with the 'd' character
- // in UTF-8.
+ // 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!
+ //
}