Start learning free

LESSON 11 OF 70 · PROJECTS: COMMAND-LINE TOOLS

Counting Characters in Kotlin: the wc Character Count

Practice Read the full explanation

Build a tiny version of the character count of the Unix wc command. Given a list of lines, return the total number of characters across all lines as an integer.

Convention: count only the characters in each line — do not count the implicit newlines that separate them. Spaces and tabs inside a line count as one character each.

Note: this is a simplified version. The real wc -m also counts the newlines, and the real wc -c counts bytes, not characters. Here the answer is simply the sum of the line lengths. The input is plain ASCII text (other alphabets are counted differently by different languages).

Function signature:

fun countChars(lines: List<String>): Int

Example:

Input: lines = ["hello", "world"]

Output: 10

"hello" has 5 chars and "world" has 5 chars, for a total of 10.

An empty line adds 0, and an empty list of lines gives 0.


Loop through the lines and accumulate line.length into an Int running total.

The full solution:
class Solution {
    fun countChars(lines: List<String>): Int {
        var total = 0
        for (line in lines) {
            total += line.length
        }
        return total
    }
}

STEP 1 OF 4
    Ctrl + Enter to check

    KOTLIN · PROJECTS: COMMAND-LINE TOOLS

    Wc V3 in Kotlin, explained

    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

    1. Create the total. A var named total starts at 0. It is a var because it grows as lines are read.
    2. Loop over the lines. A for loop takes each entry of lines as line. Write the closing brace immediately and put the next step inside.
    3. Add the length. Inside the loop, add line.length to total. Spaces and tabs are part of the line, so they are counted without any extra work.
    4. 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:

    • length is a property in Kotlin, so there are no parentheses: line.length, not line.length(). On a list or other collection the equivalent is size.
    • Int is 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, length is 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 return placed 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:

    linesresult
    ["hello", "world"]10
    ["a b", "", "\tx"]5
    []0

    In the second case the three lines have lengths 3, 0 and 2.

    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free