12. Strings — Homework solutions

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

Problem 1 — Initials

Problem. Print initials like K.R. from first and last names.

How to think about it. A name's first letter, as a string, is name[:1]. Join the pieces with + and dots.

Worked solution.

package main

import "fmt"

func main() {
    first := "Keiko"
    last := "Raharja"

    fmt.Println(first[:1] + "." + last[:1] + ".")
}

Output:

K.R.

Common mistakes.

  • Using first[1:2] instead of first[:1]. Index 1 is the second position. The first is always at index 0.
  • Using first[0] (an index) instead of first[:1] (a slice). An index gives a byte, and + cannot join a byte to a string.

Problem 2 — Stat line

Problem. Use fmt.Printf to produce Keiko Lv 7 HP 95.

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

Worked solution.

package main

import "fmt"

func main() {
    name := "Keiko"
    level := 7
    hp := 95

    fmt.Printf("%s  Lv %d   HP %d\n", name, level, hp)
}

Output:

Keiko  Lv 7   HP 95

The double space after %s and the triple space after the level match the requested output.

Common mistakes.

  • Using fmt.Println with the markers. fmt.Println does not read them, so %s is printed as the text %s.
  • Using + to join a string and an int. Go stops with a mismatched types error. fmt.Printf takes numbers directly.

Problem 3 — One-line three-line poem

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

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

Worked solution.

package main

import "fmt"

func main() {
    fmt.Println("Code is poetry,\nbugs are typos,\nfix them and ship.")
    // One call still produced three lines because the string itself contains
    // two \n characters, and each one starts a new line in the terminal.
}

Output:

Code is poetry,
bugs are typos,
fix them and ship.

Common mistakes.

  • Writing \\n instead of \n. The double backslash means a literal backslash followed by an n, which is not a newline.
  • Using backticks with \n inside. In a raw string, \n stays as those two characters.

Challenge — Last letters

Problem. Print the last three characters with a slice, and the length with len.

How to think about it. Go has no negative indexes. The last three bytes start at len(word)-3, and leaving out stop runs to the end.

Worked solution.

package main

import "fmt"

func main() {
    word := "programming"

    fmt.Println(word[len(word)-3:])
    fmt.Println(len(word))
}

Output:

ing
11

Since len(word)-3 is worked out from the word itself, the same line works for any word of at least three letters — no counting required.

Common mistakes.

  • Writing word[-3:]. Go stops with invalid argument: index -3 (constant of type int) must not be negative.
  • Writing word[len(word)-3] without the :. An index gives one byte, printed as a number, and not the last three letters.
  • Writing word[len(word):], which is a slice from the end to the end. It is the empty string.

Done?

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