mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-11-09 11:40:46 -05:00
Added Exs. 6,7 strings
This commit is contained in:
parent
faa49abb06
commit
30ef32e238
4 changed files with 67 additions and 0 deletions
40
06_strings.zig
Normal file
40
06_strings.zig
Normal file
|
@ -0,0 +1,40 @@
|
|||
//
|
||||
// Now that we've learned about arrays, we can talk about strings.
|
||||
//
|
||||
// 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!
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
const ziggy = "stardust";
|
||||
|
||||
// Use array square bracket syntax to get the letter 'd' from
|
||||
// the string "stardust" above.
|
||||
const d: u8 = ziggy[???];
|
||||
|
||||
// Use the array repeat '**' operator to make "ha ha ha".
|
||||
const laugh = "ha " ???;
|
||||
|
||||
// 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;
|
||||
|
||||
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.
|
||||
// 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!
|
||||
}
|
24
07_strings2.zig
Normal file
24
07_strings2.zig
Normal file
|
@ -0,0 +1,24 @@
|
|||
//
|
||||
// Here's a fun one: Zig has multi-line strings!
|
||||
//
|
||||
// To make a multi-line string, put '\\' at the beginning of each
|
||||
// line just like a code comment but with backslashes instead:
|
||||
//
|
||||
// const two_lines =
|
||||
// \\Line One
|
||||
// \\Line Two
|
||||
// ;
|
||||
//
|
||||
// See if you can make this program print some song lyrics.
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
const lyrics =
|
||||
Ziggy played guitar
|
||||
Jamming good with Andrew Kelley
|
||||
And the Spiders from Mars
|
||||
;
|
||||
|
||||
std.debug.print("{}\n",.{lyrics});
|
||||
}
|
|
@ -61,6 +61,7 @@ Planned exercises:
|
|||
* [x] Importing standard library
|
||||
* [x] Assignment
|
||||
* [x] Arrays
|
||||
* [x] Strings
|
||||
* [ ] If
|
||||
* [ ] While
|
||||
* [ ] For
|
||||
|
|
2
ziglings
2
ziglings
|
@ -70,6 +70,8 @@ check_it 02_std.zig "Standard Library"
|
|||
check_it 03_assignment.zig "55 314159 -11" "There are three mistakes in this one!"
|
||||
check_it 04_arrays.zig "Fourth: 7, Length: 8" "There are two things to complete here."
|
||||
check_it 05_arrays2.zig "LEET: 1337, Bits: 100110011001" "Fill in the two arrays."
|
||||
check_it 06_strings.zig "d=d ha ha ha Major Tom" "Each '???' needs something filled in."
|
||||
check_it 07_strings2.zig "Ziggy" "Please fix the lyrics!"
|
||||
|
||||
echo
|
||||
echo " __ __ _ "
|
||||
|
|
Loading…
Reference in a new issue