Start learning free

LESSON 1 OF 69 · PROJECTS: COMMAND-LINE TOOLS

cat in C#: Printing Lines with string.Join

Practice Read the full explanation

Build a tiny version of the Unix cat command. Given a list of lines, return them joined by newline characters (\n) so they print one per line.

Lines can be blank, and the list can be empty (the result is then an empty string).

Function signature:

public string Cat(List<string> lines)

Example:

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

Output: "hello\nworld\nfoo"

When printed:
hello
world
foo

Think of the output as one big string where each input line is separated from the next by a newline character. C#'s string type has a static helper that glues all elements of a collection together with a chosen separator.

Call string.Join("\n", lines) — the first argument is the separator, the second is the collection of lines.

The full solution is a one-liner:
class Solution {
    public string Cat(List<string> lines) {
        return string.Join("\n", lines);
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    C# · PROJECTS: COMMAND-LINE TOOLS

    Cat V1 in C#, explained

    Problem overview

    The Unix cat command prints text. This first version takes a List<string> and returns one string in which the lines are separated by newline characters, so printing it shows one line per row. The method is Cat.

    For ["hello", "world", "foo"] the result is "hello\nworld\nfoo". Printed, that is three rows. Lines can be blank, and an empty list gives an empty string.

    Approach

    This is the first version of the tool, and the next version builds on it.

    The tempting version loops over the lines and appends line + "\n" each time. It leaves a newline after the last line, so the result would be "hello\nworld\nfoo\n", which is not what the lesson wants. The newline belongs between two lines, not after each one.

    That "between" is exactly what string.Join does. Its first argument is a separator and its second is a collection of strings. It puts the separator between each pair of neighbours, and nothing before the first item or after the last.

    Step by step

    1. Return string.Join("\n", lines). The separator is a one-character string holding a newline. The result has no trailing newline, because a separator is only placed between items.

    The solution in C#

    class Solution {
        public string Cat(List<string> lines) {
            return string.Join("\n", lines);
        }
    }
    

    Details worth knowing:

    • string.Join accepts a list directly. It has an overload for IEnumerable<string>, and List<string> is one. You don't convert the list to an array first.
    • string and String are the same type. string is the C# keyword for System.String, so String.Join works identically.
    • Escape sequences. Inside a normal string literal, \n is one newline character. The lesson asks for \n exactly, so write it out instead of using Environment.NewLine, which is \r\n on Windows.
    • Join's two arguments have a fixed order. Putting the list first fails with error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List<string>' to 'char', because the compiler tries the overloads whose first parameter is a separator.
    • An empty list gives an empty string. Joining zero items yields "", so the special case needs no code.

    Complexity

    Time: O(total length). string.Join copies every character of every line once, plus one separator character between lines.

    Space: O(total length). The result is a new string holding all the text.

    Edge cases and common mistakes

    • A trailing newline. Appending "\n" after every line adds an extra one at the end. string.Join avoids it.
    • Blank lines. ["a", "", "b"] gives "a\n\nb". The empty entry stays in place, which is why two newlines sit side by side.
    • One blank line is the same as no lines. [""] and [] both give "", because a single item needs no separator. Two blank lines, ["", ""], give "\n".
    • Swapping the arguments. The separator comes first. string.Join(lines, "\n") doesn't compile.
    • A null list. string.Join("\n", (List<string>)null) throws an ArgumentNullException. The lesson always passes a list, possibly an empty one.
    • A null entry. An entry that is null is treated as an empty string, so [null, "x"] joins to "\nx".

    Testing

    • ["hello", "world", "foo"] gives "hello\nworld\nfoo".
    • ["a", "", "b"] gives "a\n\nb".
    • ["only"] gives "only".
    • [] 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