In the era of pay-by-the-word telegraphy, a dispatcher wants to preview a long outgoing message by keeping only its first several words, since every additional word raises the cost of the wire. A dispatch is given as a single line of words separated by exactly one space each, with no leading or trailing spaces; every word is made up only of English letters (upper- and lower-case). Given the dispatch and an integer k, produce the dispatch truncated to just its first k words, in their original order, still separated by single spaces.
A single line containing the first k words of the dispatch, joined by single spaces, with no leading or trailing space.
Example 1
Input
Telegraph lines carried news across entire nations swiftly 4
Expected
Telegraph lines carried news
Explanation
The dispatch has 8 words; keeping only the first 4 -- 'Telegraph', 'lines', 'carried', 'news' -- and joining them with single spaces gives 'Telegraph lines carried news'.
Example 2
Input
One two three four 1
Expected
One
Explanation
k = 1, so only the first word, 'One', is kept and printed on its own.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →