Start learning free

LESSON 1 OF 70 · PROJECTS: COMMAND-LINE TOOLS

cat v1 in Kotlin: Print Lines with joinToString

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:

fun cat(lines: List<String>): String

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. Kotlin's List has a method that glues all of its elements together with a chosen separator.

Call lines.joinToString("\n") — pass "\n" as the separator so each line ends up on its own line.

The full solution is a one-liner:
class Solution {
    fun cat(lines: List<String>): String {
        return lines.joinToString("\n")
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · PROJECTS: COMMAND-LINE TOOLS

    Cat V1 in Kotlin, explained

    Problem overview

    The Unix cat command prints text, one line after another. In this first version you build its core as a function: given a list of lines, return one string in which the lines are separated by newline characters (\n). Printing that string shows one line per row.

    The signature is fun cat(lines: List<String>): String. For ["hello", "world", "foo"] the result is "hello\nworld\nfoo". Blank lines count as lines, and an empty list gives an empty string.

    Approach

    You could loop over the list, append each line and a newline, and then worry about the extra newline at the end. Kotlin already has the operation you want. joinToString glues the elements of a collection into one string and puts a separator between each two of them, never before the first or after the last. The separator here is "\n".

    Step by step

    1. Call joinToString("\n") on lines and return the result. That is the whole lesson. The argument is the separator. Because it goes only between two lines, the last line gets no newline after it.

    The solution in Kotlin

    class Solution {
        fun cat(lines: List<String>): String {
            return lines.joinToString("\n")
        }
    }
    

    Calling it on a few inputs (shown with \n written out) gives:

    linesresult
    ["hello", "world", "foo"]"hello\nworld\nfoo"
    ["a", "", "b"]"a\n\nb"
    ["only"]"only"
    []""

    Printing the result

    The function returns a string; it doesn't print anything itself. To see rows, pass the result to println:

    val text = Solution().cat(listOf("hello", "world", "foo"))
    println(text)
    println(text.length)
    println(Solution().cat(listOf("x", "y")) + "|")
    

    This prints:

    hello
    world
    foo
    15
    x
    y|
    

    The length is 15 because the string holds 5 + 5 + 3 letters plus two newline characters. The | after y shows that nothing follows the last line: no trailing newline was added, so the bar sits right after it.

    Kotlin details worth knowing

    • The separator is the first parameter. joinToString has other optional parameters (a prefix, a postfix and more), so passing one string means the separator. Without any argument the separator would be ", ", which would give hello, world, foo.
    • It works on any collection. lines is a read-only List<String>, and you don't need to copy it into anything mutable first.
    • The empty list is handled for free. Joining nothing gives "", with no special case in your code.
    • "\n" is one character. In a Kotlin string literal the backslash and the n together stand for a single newline character.

    Edge cases and common mistakes

    • A trailing newline. A loop that appends line + "\n" for each line leaves a newline after the last one, and the result no longer matches "hello\nworld\nfoo". Separator-based joining avoids this.
    • Blank lines. An empty string is still an element, so ["", ""] gives "\n": two empty lines with one separator between them. Don't filter empty strings out.
    • A single line. There is nothing to separate, so the line comes back unchanged and without a newline.
    • Forgetting the argument. lines.joinToString() compiles, but it joins with commas and spaces.

    Try these inputs in the editor above. The next version builds on this function.

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