Start learning free

LESSON 3 OF 70 · PROJECTS: COMMAND-LINE TOOLS

cat -A in TypeScript: Show Tabs and Line Ends

Practice Read the full explanation

Now build the -A flag of the cat tool, as a new function. It makes whitespace visible: every tab character (\t) is replaced by the two characters ^I and every line ends with a literal $ marker (so trailing spaces become obvious). Lines are then joined with newlines.

Note: real cat -A also shows other control characters (such as ^M) and non-ASCII bytes; this version only handles tabs and line ends, like cat -T -E. A blank line becomes just $, and an empty list gives an empty string.

Function signature:

showAllCat(lines: string[]): string

Example:

Input: lines = ["hello\tworld", "foo"]

Output: "hello^Iworld$\nfoo$"

When printed:
hello^Iworld$
foo$

Another example (a blank line, trailing spaces):

Input: lines = ["a\t", "", "x  "]

Output: "a^I$\n$\nx  $"

When printed:
a^I$
$
x  $

Two transformations per line: replace every tab with "^I", then append "$". After both, join all the transformed lines with "\n".

A simple way to replace ALL tabs is line.split("\t").join("^I") (replace with a string only changes the first one). Then add "$" at the end.

The full solution:
class Solution {
    showAllCat(lines: string[]): string {
        let out: string[] = [];
        for (let i = 0; i < lines.length; i++) {
            out.push(lines[i].split("\t").join("^I") + "$");
        }
        return out.join("\n");
    }
}

STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: COMMAND-LINE TOOLS

    Cat V3 in TypeScript, explained

    Problem overview

    Version 3 of the cat project adds a function, showAllCat(lines: string[]): string, that makes whitespace visible. Every tab character is replaced by the two characters ^I, and every line ends with a $ marker, so trailing spaces show up. The lines are then joined with newlines.

    This version handles tabs and line ends only. The real cat -A also shows other control characters, but those are outside the lesson. A blank line becomes just $, and an empty list gives an empty string.

    For ["hello\tworld", "foo"] the result is "hello^Iworld$\nfoo$". For ["a\t", "", "x "] it is "a^I$\n$\nx $", which prints as:

    a^I$
    $
    x  $

    Approach: two changes per line, then join

    Each line gets the same two changes, in this order: replace every tab with ^I, then append $. After that, the changed lines are joined with "\n", as in the earlier versions.

    The only tricky part is "every tab". The string method replace with a string pattern changes only the first match. For "a\tb\tc", replace("\t", "^I") gives "a^Ib\tc", and the second tab stays. The lesson avoids that with split and join: split the line at every tab into pieces, then glue the pieces back together with ^I between them. That produces "a^Ib^Ic".

    Step by step

    1. Create an empty array out of type string[] for the changed lines.
    2. Loop over the positions with i from 0 while i < lines.length, so that lines[i] is the current line.
    3. Push the changed line: split it at the tab characters, join the pieces with ^I, and add $ at the end. The marker goes last, after the tabs are handled.
    4. After the loop, return out.join("\n"), with newlines between rows and none after the last one.

    The solution in TypeScript

    class Solution {
        showAllCat(lines: string[]): string {
            let out: string[] = [];
            for (let i = 0; i < lines.length; i++) {
                out.push(lines[i].split("\t").join("^I") + "$");
            }
            return out.join("\n");
        }
    }
    
    • split("\t") returns an array of the pieces between tabs. A line with no tab gives a one-element array, and join then returns the line unchanged. A line that starts or ends with a tab gives an empty piece at that end, which becomes a leading or trailing ^I after the join.
    • Two tabs in a row give an empty piece between them: "a\t\tb" becomes "a^I^Ib".
    • Modern JavaScript also has replaceAll("\t", "^I"), which gives the same result. The split-and-join form works in any version.
    • The + "$" applies to the whole joined line, because the method calls bind tighter than +.

    Complexity

    Each character of each line is visited a constant number of times, so time and space are both O(total length).

    Edge cases and common mistakes

    • Using replace with a string. It handles only the first tab. A line with two tabs would come out half converted.
    • Blank lines. "" has no tabs and becomes "$", so a blank line stays visible.
    • Trailing spaces. They are kept, and the $ follows them: "x " becomes "x $". That is the point of the marker.
    • Tab at the end. "a\t" becomes "a^I$".
    • Empty list. Nothing is pushed, and the result is "", not "$".
    • The marker is added per line, not once at the end of the whole output.

    Testing

    InputResult
    ["hello\tworld", "foo"]"hello^Iworld$\nfoo$"
    ["a\t", "", "x "]"a^I$\n$\nx $"
    ["a\t\tb"]"a^I^Ib$"
    []""
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free