Start learning free

LESSON 11 OF 70 · PROJECTS: COMMAND-LINE TOOLS

wc Character Count in TypeScript

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:

countChars(lines: string[]): number

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 a running total. No need to count separators.

The full solution:
class Solution {
    countChars(lines: string[]): number {
        let total = 0;
        for (const line of lines) {
            total += line.length;
        }
        return total;
    }
}

STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: COMMAND-LINE TOOLS

    Wc V3 in TypeScript, explained

    Problem overview

    The third version of the wc project counts characters. countChars(lines: string[]): number returns the sum of the lengths of all the lines. Newlines between lines are not counted: only the characters inside each line are, and a space or a tab counts as one character like any other.

    For ["hello", "world"] the answer is 10, because each line has 5 characters. An empty line adds 0, and an empty array gives 0. The input is plain ASCII text.

    Approach

    This is the same shape as the word count from the previous version, with a simpler inner step. There is no need to cut the lines into pieces: a string already knows its size through its length property. Visit every line, add its length to a running total, and return the total.

    Step by step

    1. Create the total. Declare total with let and start it at 0, since nothing has been counted yet.
    2. Visit every line with a for (const line of lines) loop. The next step goes inside it.
    3. Add the line's length. line.length is the number of characters in that one line, so total += line.length grows the total by exactly that much.
    4. Return the total after the loop. Returning inside the loop would stop after the first line.

    The solution in TypeScript

    class Solution {
        countChars(lines: string[]): number {
            let total = 0;
            for (const line of lines) {
                total += line.length;
            }
            return total;
        }
    }

    Notes on the language:

    • length is a property, not a method: write line.length, not line.length(). The same name is used for arrays, which is why lines.length is the number of lines and line.length is the number of characters.
    • for...of over an array gives the values themselves. This is different from for...in, which gives the indices as strings.
    • TypeScript infers number for total from the 0, so no annotation is needed here.
    • Strings in JavaScript are made of UTF-16 code units, and length counts those. For ASCII text that is the same as counting characters. A character outside the basic range, such as an emoji, can count as 2: "😀".length is 2, while [..."😀"].length is 1. The lesson's input is ASCII, so this does not come up.

    Complexity

    The loop visits each line once and reading length takes constant time, so the running time is O(m) for m lines. It does not depend on how long the lines are. The extra space is O(1): one number.

    Edge cases and common mistakes

    • Counting separators. The real wc also counts the newline after each line. This version does not, so adding lines.length to the total gives the wrong answer. For ["hello", "world"] the answer is 10, not 12.
    • Whitespace. Spaces and tabs inside a line count. "a\tb" has length 3. Do not trim the lines first.
    • Empty input. An empty line adds 0, and an empty array leaves the total at its starting value of 0.
    • Using the wrong length. total += lines.length inside the loop compiles and runs, but it counts lines, not characters.

    Testing

    InputResult
    ["hello", "world"]10
    ["a b\tc", "", "xy"]7
    []0

    The next version builds on this one.

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