Start learning free

LESSON 3 OF 69 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool cat v3 in C++: Show Special Characters (-A) with Tabs and $

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:

string showAllCat(vector<string> lines)

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  $

There's no built-in replace, so walk each line character by character. When you see a '\t', append "^I"; otherwise append the character itself. After the inner loop, append "$".

Inner transformation:
string transformed;
for (char c : line) {
    if (c == '\t') transformed += "^I";
    else transformed += c;
}
result += transformed + "$";


The full solution:
class Solution {
public:
    string showAllCat(vector<string> lines) {
        string result;
        for (int i = 0; i < lines.size(); i++) {
            if (i > 0) result += "\n";
            string line = lines[i];
            string transformed;
            for (char c : line) {
                if (c == '\t') transformed += "^I";
                else transformed += c;
            }
            result += transformed + "$";
        }
        return result;
    }
};

STEP 1 OF 10
    Ctrl + Enter to check

    C++ · PROJECTS: COMMAND-LINE TOOLS

    Cat V3 in C++, explained

    Problem overview

    The -A flag of cat makes invisible characters visible. In this version, showAllCat handles two of them: every tab character becomes the two characters ^I, and every line gets a $ at its end, so trailing spaces become easy to see. Lines are then joined with newlines, with none after the last. Real cat -A also marks other control characters and non-ASCII bytes. This version does only tabs and line ends.

    For ["hello\tworld", "foo"] the result is "hello^Iworld$\nfoo$". A second example: ["a\t", "", "x "] gives "a^I$\n$\nx $". The blank line becomes just $, and the two trailing spaces of x now sit right in front of the marker. An empty vector gives "".

    Approach: rebuild each line character by character

    The skeleton is the one from the earlier versions: loop over the positions, put a newline before every row except the first, and append the row. The new work is turning a line into its visible form.

    A string can't have a character replaced by two, so the function builds a new string, transformed, instead of editing the line in place. It walks over the line's characters and appends each one to transformed, except that a tab is replaced by ^I. After the walk, the marker $ is added once, at the end of the row.

    The marker belongs to the row and is added once per line, not once per character. That is why it is appended after the inner loop.

    The same loop shape would work for other replacements too, such as showing a carriage return as ^M, by adding another else if branch. This lesson stops at tabs. The next version of the tool builds on the earlier ones and is not part of this one.

    Step by step

    1. Create an empty string named result for the finished rows.
    2. Loop over the positions with i from 0 while i < lines.size(). The position tells which line to take and whether it is the first.
    3. Add the separator. When i is above 0, append "\n" to result.
    4. Copy the line into a string named line. It is only a short name for the next steps.
    5. Create an empty string named transformed for the visible version of this line.
    6. Loop over the characters with for (char c : line). A range-based loop visits every character in order and needs no index.
    7. Replace a tab. When c is '\t', append "^I" to transformed. A single character uses single quotes, and a string of several characters uses double quotes.
    8. Keep every other character. The else appends c unchanged.
    9. Close the row. After the inner loop, append transformed and the marker "$" to result.
    10. Return result after the outer loop.

    The solution in C++

    class Solution {
    public:
        string showAllCat(vector<string> lines) {
            string result;
            for (int i = 0; i < lines.size(); i++) {
                if (i > 0) result += "\n";
                string line = lines[i];
                string transformed;
                for (char c : line) {
                    if (c == '\t') transformed += "^I";
                    else transformed += c;
                }
                result += transformed + "$";
            }
            return result;
        }
    };
    

    The C++ details worth a closer look:

    • Single quotes and double quotes. '\t' is one char, and "^I" is a string of two characters. Comparing c == '\t' compares characters. transformed += c appends one character, while transformed += "^I" appends a string. Both forms work with +=.
    • The caret is plain text. ^I is just the characters ^ and I. It is the traditional way to write the control character with code 9, which is the tab.
    • Range-based for. for (char c : line) gives a copy of each character. Changing c would not change line, and the loop doesn't need to, because the output goes to a new string.
    • Copying a line. string line = lines[i]; copies the text. The copy is not needed for correctness, since lines[i] could be used directly, but the lesson uses it to keep the loop readable.
    • Concatenation. transformed + "$" builds a temporary string, which += then appends. Since transformed is a string, adding a string literal to it is allowed.

    One more point about the order of the work: the replacement happens before the marker is added. The marker is never passed through the tab check, so it can't be changed by it, and it always ends the row.

    Complexity

    Time: O(total length). Every character of every line is visited once, and every append is amortized constant time.

    Space: O(total length). The result holds all rows, and each tab takes one more character in the output than in the input.

    Edge cases and common mistakes

    • Blank line. It produces just $. The inner loop runs zero times and the marker is still added.
    • Empty vector. No rows, so the result is "". No marker is added, because markers belong to lines.
    • Trailing spaces. They are kept as they are, in front of the marker: "x " becomes "x $".
    • Several tabs. Each one is replaced: "\t\t" becomes "^I^I$".
    • A $ already in the text. It is not changed. "a$b" becomes "a$b$", so a dollar sign in the middle is data and only the last one is the marker.
    • Putting the marker in the wrong place. Adding $ inside the inner loop would add it after every character.
    • A newline after the last row. The separator goes before a row when i > 0, so the result ends with the final $.

    Testing

    Each result is the function's output, with newlines written as \n:

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