# Stripping off extra spaces from a string

You are given a string. Output the string with one space per words.

# Challenge

Input will be a string (not null or empty), surrounded with quotes(") sent via the stdin. Remove leading and trailing spaces from it. Also, if there are more than one space between two words (or symbols or whatever), trim it to just one space. Output the modified string with the quotes.

# Rules

• The string will not be longer than 100 characters and will only contain ASCII characters in range (space) to ~(tilde) (character codes 0x20 to 0x7E, inclusive) except ",i.e, the string will not contain quotes(") and other characters outside the range specified above. See ASCII table for reference.
• You must take input from the stdin( or closest alternative ).
• The output must contain quotes(").
• You can write a full program, or a function which takes input (from stdin), and outputs the final string

# Test Cases

"this  is  a    string   "         --> "this is a string"

"  blah blah    blah "             --> "blah blah blah"

"abcdefg"                          --> "abcdefg"

"           "                      --> ""

"12 34  ~5 6   (7, 8) - 9 -  "     --> "12 34 ~5 6 (7, 8) - 9 -"


# Scoring

This is code golf, so the shortest submission (in bytes) wins.

• You say must take input from stdin, and later you say ...or a function which takes input, and outputs the final string. Does this mean the function must take input from stdin as well? May 20 '15 at 15:35
• @blutorange , Yes. Edited to clarify it. May 20 '15 at 15:36
• " "aa" " --> ""aa"" (are quotes valid inside the input string?) May 20 '15 at 15:36
• @edc65 , Good point. The answer to that is no. Edited to clarify it. May 20 '15 at 15:51
• Some answers are processing string including the double quotes: "   this  ", others process a once double quoted string which reaches the code with the double quotes already stripped off:  this . This way the answers and the languages'/authors' efficiencies are not really comparable. @CoolGuy, could you firmly clarify the requirement on this? May 22 '15 at 10:49

# Python 3, 37 bytes

print("%r"%' '.join(input().split()))


Based of user12205's python2 answer. I've replaced the %s with a %r so that single quotes are printed around the string automatically.

Try it online!

# Nim, 72 bytes

import strutils


Try it online!

Ignoring the IO restrictions as many answers do, 58 bytes:

import strutils


Try it online!

# C (gcc), 82 bytes

char*s=&s+9;main(t){for(gets(s);*s;s++)*s>32|(s[1]>32&&s[2]&&t)&&putchar(*s,--t);}


Try it online!

# K (ngn/k), 211816 15 bytes

" "/(~#:')_" "\


Try it online!

• " "\ split (implicit) input on spaces
• (~#:')_ drop empty strings
• " "/ join remaining items with spaces

# Factor, 23 bytes

[ [ 32 = ] " "compact ]


Try it online!

Factor has a combinator called compact that does this. From the documentation:

Generate a new sequence where all runs of elements for which the predicate returns true are replaced by a single instance of elt. Runs at the beginning or end of the sequence for which the predicate returns true are removed.

# Python2, 28 Bytes

lambda s:" ".join(s.split())


## Explanation

lambda s


Anonymous function which takes as input s.

s.split()


Returns a list of the words (which are separated by arbitrary strings of whitespace characters) of the string s.

" ".join(...)


Joins list back into a string, with each word separated by a space (" ").