Problem overview
This version of the wc project counts characters. The function countChars takes a List<String> of lines and returns the total number of characters in all of them as an Int.
The lesson uses a simple convention: only the characters inside each line count, not the newlines that separate lines. Spaces and tabs inside a line count as one character each. So the answer is just the sum of the line lengths. The real wc tool counts newlines too (with -m) or counts bytes (with -c), but this version doesn't. The input is plain ASCII, where one character is one byte and one Char in Kotlin.
For lines = ["hello", "world"] the answer is 10, because each word has 5 characters. An empty line adds 0, and an empty list gives 0.
Approach: add up the lengths
Every Kotlin String has a length property with its number of characters. You keep a running total, visit each line, and add that line's length. Nothing else is needed: there is no trimming and no special case for empty lines, because an empty string has a length of 0 and adds nothing.
This is the same shape as the previous version, a total and a loop, with a simpler step inside the loop.
Step by step
- Create the total. A
varnamedtotalstarts at 0. It is avarbecause it grows as lines are read. - Loop over the lines. A
forloop takes each entry oflinesasline. Write the closing brace immediately and put the next step inside. - Add the length. Inside the loop, add
line.lengthtototal. Spaces and tabs are part of the line, so they are counted without any extra work. - Return the total after the loop. The return belongs outside the braces, so that it runs once after every line has been added.
The solution in Kotlin
class Solution {
fun countChars(lines: List<String>): Int {
var total = 0
for (line in lines) {
total += line.length
}
return total
}
}
Some details to notice:
lengthis a property in Kotlin, so there are no parentheses:line.length, notline.length(). On a list or other collection the equivalent issize.Intis a 32-bit integer, and an overflow wraps around silently. That only matters for totals above about 2.1 billion characters, far beyond what a list of lines in memory will hold in this lesson.- For ASCII text,
lengthis the number of characters you see. Kotlin strings are sequences of UTF-16 units, so some characters outside ASCII, such as many emoji, count as 2. The lesson's input avoids that.
Edge cases and common mistakes
- Counting the newlines. Adding 1 for each line would match the real
wc -m, but this lesson's convention leaves them out.["hello", "world"]gives 10, not 11. - Trimming the lines. Trimming was needed for words, but here a leading space is a character and counts.
"a b"has length 3 and"\tx"has length 2. - Returning inside the loop. A
returnplaced inside the braces would stop after the first line. - Empty input. An empty list never enters the loop, so the function returns the starting 0.
Testing
These results come from running the reference solution:
| lines | result |
|---|---|
["hello", "world"] | 10 |
["a b", "", "\tx"] | 5 |
[] | 0 |
In the second case the three lines have lengths 3, 0 and 2.
