14
\$\begingroup\$

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.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – blutorange
    May 20 '15 at 15:35
  • \$\begingroup\$ @blutorange , Yes. Edited to clarify it. \$\endgroup\$
    – Spikatrix
    May 20 '15 at 15:36
  • 2
    \$\begingroup\$ " "aa" " --> ""aa"" (are quotes valid inside the input string?) \$\endgroup\$
    – edc65
    May 20 '15 at 15:36
  • \$\begingroup\$ @edc65 , Good point. The answer to that is no. Edited to clarify it. \$\endgroup\$
    – Spikatrix
    May 20 '15 at 15:51
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – manatwork
    May 22 '15 at 10:49

36 Answers 36

1
2
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

Nim, 72 bytes

import strutils
echo'"',stdin.readAll[1..^2].splitWhitespace.join" ",'"'

Try it online!

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

import strutils
echo stdin.readAll.splitWhitespace.join" "

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

K (ngn/k), 21 18 16 15 bytes

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

Try it online!

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

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.

\$\endgroup\$
-1
\$\begingroup\$

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 (" ").

\$\endgroup\$
1
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.