Given a line of text, split it into words and join them with commas.
Words may be separated by one or more spaces or tabs. Ignore extra whitespace and preserve the original order of the words.
Input: A single line of text containing at least one word.
Output: Print the words separated by commas, without additional spaces.
Input:
hello world
Output:
hello,world
Explanation: Keep the words in their original order and put one comma between each pair of neighboring words.
Input:
alpha beta
Output:
alpha,beta
Explanation: Ignore leading and trailing whitespace. Repeated spaces separate words without creating empty words; join the words with one comma between them.
Input:
blue
Output:
blue
Explanation: There is only one word, so print it unchanged without a comma.
Input:
red green yellow
Output:
red,green,yellow
Explanation: Tabs and spaces both separate words. Keep the words in order and replace each separator with one comma.
Input:
one two three four
Output:
one,two,three,four
Explanation: Keep the words in their original order and put one comma between each pair of neighboring words.
How can you separate words without creating empty words when multiple spaces occur?
A shell separates user input such as echo hello world into a command and its arguments. This process is called tokenization, a fundamental step in building command-line tools.
System 1: Build a Command-Line Shell
Optional application of the idea. You write the System solution separately.
Learn how a shell finds an executable by checking possible file paths.
hello,world