Skip to content

Exercises

This section collects the exercises for Chapter 14.

This section collects the exercises for Chapter 14.

Exercise 14-1. Print the integer 255 in decimal, hexadecimal, and binary.

Exercise 14-2. Define a struct named Point with fields x and y. Print a value of this type with {any}.

Exercise 14-3. Write a program that uses std.debug.assert to check that a value is greater than zero.

Exercise 14-4. Write normal output to standard output and diagnostic output with std.debug.print.

Exercise 14-5. Compare two strings with std.mem.eql.

Exercise 14-6. Search for a substring with std.mem.indexOf.

Exercise 14-7. Split a string into words separated by spaces.

Exercise 14-8. Fill an array of bytes with zeroes and print it.

Exercise 14-9. Create an ArrayList(u32) and append the numbers from 1 through 10.

Exercise 14-10. Build a string with ArrayList(u8).

Exercise 14-11. Remove an element with orderedRemove.

Exercise 14-12. Remove an element with swapRemove and compare the result.

Exercise 14-13. Create a map from strings to integers.

Exercise 14-14. Count how many times each word appears in an array.

Exercise 14-15. Iterate through a map and print all key-value pairs.

Exercise 14-16. Replace StringHashMap with AutoHashMap using integer keys.

Exercise 14-17. Print several numbers in aligned columns.

Exercise 14-18. Format a string into a fixed buffer with std.fmt.bufPrint.

Exercise 14-19. Format a string with std.fmt.allocPrint and free it.

Exercise 14-20. Write a helper function that formats an error message into a caller-provided buffer.

Exercise 14-21. Create a file named hello.txt and write one line to it.

Exercise 14-22. Read hello.txt into a fixed buffer and print it.

Exercise 14-23. Read a file with readFileAlloc and free the returned memory.

Exercise 14-24. Iterate over the current directory and print each entry name.

Exercise 14-25. Print all command-line arguments.

Exercise 14-26. Write a program that accepts a name and prints a greeting.

Exercise 14-27. Read an environment variable and handle the missing case.

Exercise 14-28. Run zig version as a child process and print its output.

Exercise 14-29. Write a test for a function that returns the larger of two integers.

Exercise 14-30. Write a test that compares two byte slices.

Exercise 14-31. Write a test that expects a specific error.

Exercise 14-32. Use std.testing.allocator in a test and free all allocated memory.