Double Space

April 4, 2017

Today’s exercise is simple, but some languages make it hard:

Write a program that returns an input string with each space character in the string doubled. For instance, the string “hello hello” (with one space between the two words) is transformed to “hello  hello” (with two spaces between the two words).

That may not come out right in all browsers; here is the transformation again, with space characters replaced by plus signs for visibility, using a constant-space font:

"hello+hello" => "hello++hello"

Depending on your language, that might be an easy task, or a hard one. You may have to deal with memory allocation, or non-mutable strings, or appends that blow up to quadratic time.

Your task is to write a program that doubles the space characters in its input. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

Pages: 1 2

15 Responses to “Double Space”

  1. As a perl programmer this one is trivial:

    sub ds{shift=~s{ }{  }rg}
    

    if you don’t want to use s{} could do

    sub ds{join'',map{$_,' 'eq$_?' ':''}split'',shift}
    
  2. Pavel Tumilovich said

    Clojure:

    (defn dup_space
      [input]
      (apply str
             (map
               (fn [c] (if(= \space c) 
                         "  " 
                         (str c)))
               input)))
    
  3. Paul said

    Also not too difficult in Python.

    def double_space(txt):
        return txt.replace(" ", "  ")
    
  4. javascript:

    function double_space(txt){
    return txt.replace(/ /g,’ ‘);
    }

  5. Jussi Piitulainen said

    Read-write Scheme.

    (setlocale LC_ALL "") ;sigh (this is in guile 2.0.9)
    
    (define (double-space-port in out)
      (let ((o (read-char in)))
        (or (eof-object? o)
            (begin
              (write-char o out)
              (case o ((#\space) (write-char o out)))
              (double-space-port in out)))))
    
    (define (double-spaced-string s)
      (let ((out (open-output-string)))
        (double-space-port (open-input-string s) out)
        (get-output-string out)))
    
    (write (double-spaced-string "Taas mennään.  Ja äätkin näkyy!"))
    (newline)
    

    Read-write Python.

    import io
    
    def double_space_io(inio, outio):
        while True:
            o = inio.read(1)
            if not o: return
            outio.write(o)
            if o in ' ': outio.write(o)
    
    def double_space_str(s):
        outio = io.StringIO()
        double_space_io(io.StringIO(s), outio)
        return outio.getvalue()
    
    print(repr(double_space_str("Taas mennään.  Ja äätkin näkyy!")))
    
  6. Rutger said

    Took the Python generator approach. We can feed the double_space generator with characters and they get doubled up.

    import time
    
    def double_space(g):
    	while True:
    		x = next(g)
    		if x == ' ':
    			yield x
    		yield x
    
    def text_streamer(text):
    	i = 0
    	while i < len(text):
    		yield(text[i])
    		i += 1
    		time.sleep(1/10)
    
    text = "Hello  from Python !"
    s = text_streamer(text)
    
    for v in double_space(s):
    	print(v,)
    
  7. Steve said

    MUMPS:

    r !,word,! f i=1:1:$l(word,” “) w:i>1 ” ” w $p(word,” “,i)

    hello, world!
    hello, world!

  8. Globules said

    A Haskell version. (Note: cat -e prints a ‘$’ at the end of a line.)

    main :: IO ()
    main = interact (concatMap (\c -> if c == ' ' then "  " else [c]))
    
    $ printf " foo  bar   \nbaz  zot\n" | ./dblspc | cat -e
      foo    bar      $
    baz    zot$
    
  9. Jeff said

    Nothing difficult for Ruby

    `def double_space(string)
    return string.gsub(/\s/, ‘ ‘)
    end

    puts double_space(“hello word”)`

  10. dup (‘ ‘:xs) = ‘ ‘:’ ‘:dup xs
    dup ( x :xs) = x :dup xs
    dup [] = []

  11. for non streaming algorithms the former maybe slow and memory expensive then, we can use ByteStrings (lazy or strict, depends)

    {-# LANGUAGE OverloadedStrings #-}
    import qualified Data.ByteString.Lazy.Char8 as BS

    dup’ = BS.concatMap d
    where d ‘ ‘ = " "
    d x = BS.singleton x

  12. […] how easy your favorite programming language makes it to work with strings. The problem is to double every blank in a string. That’s pretty simple but some languages make you deal with string allocation or perhaps, in […]

  13. jleechpe said

    Powershell makes it easy:

    Function Set-DoubleSpace ($a) {$a-replace” “,” “}

    or from STDIN

    (read-host)-replace” “,” “

  14. Aurelio said

    in Java:
    public String doubleSpaces(String in) {

    return in.replace(” “, ” “);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: