12
2
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.
1You 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 fromstdin
as well? – blutorange – 2015-05-20T15:35:08.720@blutorange , Yes. Edited to clarify it. – Spikatrix – 2015-05-20T15:36:30.760
2
" "aa" "
-->""aa""
(are quotes valid inside the input string?) – edc65 – 2015-05-20T15:36:36.707@edc65 , Good point. The answer to that is no. Edited to clarify it. – Spikatrix – 2015-05-20T15:51:37.620
Please see MickeyT's comment on my answer. Is what he proposes valid? In R, returned results are implicitly printed, but in my answer I've explicitly printed to stdout.
– Alex A. – 2015-05-20T19:30:04.603@AlexA. , The output needs to be seen in the stdout. It doesn't matter if it is implicitly or explicitly printed. – Spikatrix – 2015-05-21T08:40:14.960
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? – manatwork – 2015-05-22T10:49:23.977@manatwork , I don't know. Do many answers take advantage of it? If so, I think it is better to leave both ways permitted. – Spikatrix – 2015-05-23T06:49:23.530
@CoolGuy, yeah, now that we already have 20 answers, is indeed a bit late to impose a restriction. Though in my opinion this way the challenge is ruined. :( – manatwork – 2015-05-23T13:58:52.387