12. Strings — Homework solutions

The .lua solution files are in exercises/12/homework/solutions/.

Problem 1 — Loud and quiet

Problem. Print a name three times: original, upper, lower.

How to think about it. You need two new functions: string.upper(s) and string.lower(s). Neither changes the original variable, so all three prints reference the same name.

Worked solution.

local name = "Keiko"

print(name)
print(string.upper(name))
print(string.lower(name))

Common mistakes.

  • Writing name = string.upper(name) between prints. That overwrites the original value, so a later print uses the changed text instead of "Keiko". Keep the original untouched and only transform it inside each print call.

Problem 2 — Stat line

Problem. Use string.format to produce Keiko Lv 7 HP 95.

How to think about it. Three values, three placeholders: %s for the name, %d for the level, %d for HP. The labels (Lv, HP) and the spaces between fields live inside the format string itself.

Worked solution.

local name = "Keiko"
local level = 7
local hp = 95

print(string.format("%s  Lv %d   HP %d", name, level, hp))

The double-space after %s and the triple-space after the level match the requested output. Adjust the spaces to taste.

Common mistakes.

  • Using %d for a string or %s for a number. The wrong placeholder produces an error like bad argument to format. Pair %s with strings and %d with whole numbers.
  • Forgetting to pass enough arguments. Three placeholders need three values.

Problem 3 — One-line three-line poem

Problem. One print call, three lines of output, separated by \n.

How to think about it. Inside a regular string, \n is the newline character, so one string with two \ns prints as three lines. The comment has to say why: print writes whatever it is handed, and this string already contains newline characters.

Worked solution.

print("Code is poetry,\nbugs are typos,\nfix them and ship.")
-- One print call still produced three lines because the string itself
-- contained two `\n` characters, each of which Lua replaces with a
-- newline before sending to the terminal.

Common mistakes.

  • Writing \\n instead of \n. The double backslash means a literal backslash followed by an n, which Lua does not treat as a newline.

Challenge — Title block

Problem. Print a title with a dashed border above and below. The border length is computed from the title length, not typed out.

How to think about it. The border has to be #title + 4 characters long (two dashes on each side of " TITLE "). Build a row of dashes that length with string.rep("-", n), and build the middle row by gluing dashes, spaces, the title, more spaces, and dashes.

Worked solution.

local title = "INVENTORY"

local border = string.rep("-", #title + 4)
local middle = "-- " .. title .. " --"

print(border)
print(middle)
print("Inside text goes here")
print(middle)
print(border)

The output for title = "INVENTORY":

-------------
-- INVENTORY --
Inside text goes here
-- INVENTORY --
-------------

Change title to "SHOP" and the border shrinks. That is why you use #title instead of counting dashes by hand.

Common mistakes.

  • Hard-coding the number of dashes. The challenge is to compute the length from #title.
  • Confusing #title + 4 (the border length in characters) with the string content. The first is a number passed to string.rep; the second is the actual text.

Done?

Next is Numbers and math, then Getting input, then the Part 3 mini-project.