Problem overview
The function countChars takes a slice of strings, lines, and returns the total number of characters across all of them. This is a simplified take on the Unix wc command: only the characters inside each line are counted, and the newlines that would separate the lines are not. Spaces and tabs count as one character each.
For ["hello", "world"] the answer is 10, because each word has 5 characters. An empty line adds 0, and an empty slice gives 0. The input is plain ASCII.
Approach
The structure is the same as in the word-count version: keep a running total, visit every line, add something about that line. What changes is what gets added. Here it is the length of the line, which in Go is len(line).
The loop is also written differently. Instead of range, it counts the position i from 0 up to len(lines) - 1 and reads lines[i]. Both styles do the same work. The indexed form is the one to reach for when the position itself matters, and it is worth knowing because Go has no while loop: for in its three-part form covers it.
Step by step
- Create the total.
total := 0declares anint. It is the answer for an empty slice, since the loop then doesn't run. - Write the loop header.
for i := 0; i < len(lines); i++has three parts: the start, the condition checked before each pass, and the step after each pass. The last valid position islen(lines) - 1, so the condition uses<, not<=. - Add the length. Inside the loop,
len(lines[i])is the size of the current line, andtotal +=adds it. - Return the total after the loop's closing brace, so it runs once, after all lines are counted.
The solution in Go
func countChars(lines []string) int {
total := 0
for i := 0; i < len(lines); i++ {
total += len(lines[i])
}
return total
}
The detail that matters most is what len measures on a string. len counts bytes, not characters. For ASCII text the two are equal, which is why this solution passes the lesson's tests. With other alphabets they differ: len("héllo") is 6, because é takes two bytes in UTF-8, although the word has 5 characters. To count characters (runes) you would use utf8.RuneCountInString from the unicode/utf8 package, or len([]rune(s)). The lesson only gives ASCII, so len is enough.
Other notes:
- No newline is added. The total is the sum of the line lengths and nothing else, so
["hello", "world"]gives 10, not 11. - Tabs count once.
["a b", "\tx", ""]gives 5: three characters, then two (a tab andx), then none. i++is a statement. In Go it can't be used inside an expression, and there is no++i.
Complexity
Time: O(m), where m is the number of lines. In Go, len of a string reads a stored size and doesn't scan the characters, so each line costs constant time and the loop does one step per line.
Space: O(1). Only total and the loop counter are kept.
Edge cases and common mistakes
- Empty slice or empty lines. Both add nothing, so the answer is 0 when there are no characters.
- Off-by-one in the loop. Writing
i <= len(lines)readslines[len(lines)], which is out of range and makes the program panic. - Counting separators. The real
wccounts the newline between lines; this lesson doesn't, so don't add 1 per line. - Non-ASCII text. Because
lenreturns bytes, the total for["héllo"]is 6, not 5.
Testing
["hello", "world"]gives10.["a b", "\tx", ""]gives5.[]gives0.
