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
- 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.Joinaccepts a list directly. It has an overload forIEnumerable<string>, andList<string>is one. You don't convert the list to an array first.stringandStringare the same type.stringis the C# keyword forSystem.String, soString.Joinworks identically.- Escape sequences. Inside a normal string literal,
\nis one newline character. The lesson asks for\nexactly, so write it out instead of usingEnvironment.NewLine, which is\r\non 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.Joinavoids 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
nulllist.string.Join("\n", (List<string>)null)throws anArgumentNullException. The lesson always passes a list, possibly an empty one. - A
nullentry. An entry that isnullis 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"".
