44
\$\begingroup\$

Full width text is text that has a space after every character, including the last one. For instance, the first sentence of this question becomes:

F u l l   w i d t h   t e x t   i s   t e x t   t h a t   h a s   a   s p a c e   a f t e r   e v e r y   c h a r a c t e r ,   i n c l u d i n g   t h e   l a s t   o n e . 

Write a program that takes a line in text from standard input and outputs it as full-width text to standard out.

Leaderboard

var QUESTION_ID=75979,OVERRIDE_USER=52353;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • 6
    \$\begingroup\$ Usually you should allow functions too, or you exclude a lot of languages (e.g. JavaScript). \$\endgroup\$ – wizzwizz4 Mar 22 '16 at 18:00
  • 8
    \$\begingroup\$ We have a few defaults for I/O that are based on community consensus. While you are entitled to override them, insisting on STDIN/STDOUT for I/O invalidates a bunch of answers (which assumed that the defaults apply) and make the task downright impossible in other languages (they don't have standard streams). \$\endgroup\$ – Dennis Mar 22 '16 at 21:22
  • 53
    \$\begingroup\$ That is not what fullwidth text is. \$\endgroup\$ – BlueRaja - Danny Pflughoeft Mar 22 '16 at 21:53
  • 4
    \$\begingroup\$ @BlueRaja-DannyPflughoeft is right. Full Width text is about underlying character encoding ( 2 bytes encoded ) required by some language ( i.e. ideograms ). In Unicode the notion of half and full size is called Unicode block \$\endgroup\$ – Ludovic Frérot Mar 23 '16 at 9:27
  • 2
    \$\begingroup\$ @LudovicFrérot Actually, these are not ideograms, these are chinese english letters. \$\endgroup\$ – Erik the Outgolfer Jul 1 '16 at 8:29

128 Answers 128

1
\$\begingroup\$

Perl 5, 11 10 + 1 flags = 11 bytes

really, the same as the Perl 6 one, but the sed syntax changed.

s/./$& /g

example:

perl -pe "s/./$& /g" <<< abc

old:

perl -pne "s/(.)/\1 /g" <<< abc

\$\endgroup\$
  • \$\begingroup\$ Can you save a byte by using s/./$& /gp? \$\endgroup\$ – Neil Mar 22 '16 at 22:01
  • 1
    \$\begingroup\$ The -n flag is unnecessary, isn't it? \$\endgroup\$ – Doorknob Mar 22 '16 at 22:40
  • \$\begingroup\$ @Doorknob is indeed correct, you can omit -p, also, how flexible is the output format? If a leading space is acceptable s// /g works too! \$\endgroup\$ – Dom Hastings Mar 23 '16 at 8:47
  • \$\begingroup\$ I don't think I'm allowed to have a leading space :). Thanks @Doorknob \$\endgroup\$ – Ven Mar 23 '16 at 9:15
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 5 chars / 7 bytes

ᴉü⬭+⬭

Try it here (Firefox only).

Short and sweet. Get input split along chars and join with space.

\$\endgroup\$
  • \$\begingroup\$ This doesn't produce a trailing space. \$\endgroup\$ – Conor O'Brien Mar 23 '16 at 15:48
  • \$\begingroup\$ I think I fixed it, but I'm on my phone. \$\endgroup\$ – Mama Fun Roll Mar 23 '16 at 16:12
1
\$\begingroup\$

J, 8 bytes

,@,.&' '

,. is the stitch command, and ' ' , 'string' creates a table with a space following each character. , takes these items out of the table when used as a monad, and @ composes this function with our other function. Use:

   fullWidth =: ,@,.&' '
   fullWidth 'this text is full width!'
   fullWidth 'this text is full width!'
t h i s   t e x t   i s   f u l l   w i d t h ! 

Great thanks to Zgarb who helped me make this answer.

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

PowerShell, 32 bytes

-join([char[]]$args[0]|%{"$_ "})

Explanation

Take in the input to the function and convert it to a char array. Add one space to each character and rejoin. Nothing really fancy here.

\$\endgroup\$
  • \$\begingroup\$ Save a couple bytes by just joining it directly and encapsulating in a script block in a string -- "$([char[]]$args[0]-join' ') " \$\endgroup\$ – AdmBorkBork Jul 26 '16 at 16:02
1
\$\begingroup\$

Prelude, 11 bytes

?(!8^+^+!?)

This needs a spec-compliant interpreter which reads and writes character codes (instead of integers).

The program is fairly simple.

?(...?)

This sets up a while(read() != EOF) loop. Then we simply have:

!   Print input character.
8   Push 8.
^+  Duplicate, add. Gives 16.
^+  Duplicate, add. Gives 32.
!   Print space.
\$\endgroup\$
1
\$\begingroup\$

Pip, 5 bytes

_.sMq

The special variable q reads a line of stdin. We Map to it the lambda function _.s, which concatenates a space to each character. By default, the resulting list is concatenated and autoprinted.

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

Ouroboros, 11 bytes

i.0<5*(o32o

Ouroboros is an esolang in which every line of code represents a snake eating its tail. It draws a fair amount of inspiration from 2D languages like Befunge and ><>.

The i command reads a character, or -1 for EOF. We duplicate this, check if it's less than 0, and multiply the resulting 1 (true) or 0 (false) by 5.

  • If we have reached EOF, the top of the stack is now 5. The ( command tells the snake to eat that many characters of its tail. This results in swallowing the execution pointer (currently on (, the fifth-last character), which terminates the program.
  • Otherwise, 0 characters are eaten, and execution continues. We have a copy of the input ASCII code on the stack, which we output via o; then output a space (ASCII 32) and loop back to the beginning.

Try it out

// Define Stack class
function Stack() {
  this.stack = [];
  this.length = 0;
}
Stack.prototype.push = function(item) {
  this.stack.push(item);
  this.length++;
}
Stack.prototype.pop = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack.pop();
    this.length--;
  }
  return result;
}
Stack.prototype.top = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack[this.length - 1];
  }
  return result;
}
Stack.prototype.toString = function() {
  return "" + this.stack;
}

// Define Snake class
function Snake(code) {
  this.code = code;
  this.length = this.code.length;
  this.ip = 0;
  this.ownStack = new Stack();
  this.currStack = this.ownStack;
  this.alive = true;
  this.wait = 0;
  this.partialString = this.partialNumber = null;
}
Snake.prototype.step = function() {
  if (!this.alive) {
    return null;
  }
  if (this.wait > 0) {
    this.wait--;
    return null;
  }
  var instruction = this.code.charAt(this.ip);
  var output = null;
  console.log("Executing instruction " + instruction);
  if (this.partialString !== null) {
    // We're in the middle of a double-quoted string
    if (instruction == '"') {
      // Close the string and push its character codes in reverse order
      for (var i = this.partialString.length - 1; i >= 0; i--) {
        this.currStack.push(this.partialString.charCodeAt(i));
      }
      this.partialString = null;
    } else {
      this.partialString += instruction;
    }
  } else if (instruction == '"') {
    this.partialString = "";
  } else if ("0" <= instruction && instruction <= "9") {
    if (this.partialNumber !== null) {
      this.partialNumber = this.partialNumber + instruction;  // NB: concatenation!
    } else {
      this.partialNumber = instruction;
    }
    next = this.code.charAt((this.ip + 1) % this.length);
    if (next < "0" || "9" < next) {
      // Next instruction is non-numeric, so end number and push it
      this.currStack.push(+this.partialNumber);
      this.partialNumber = null;
    }
  } else if ("a" <= instruction && instruction <= "f") {
    // a-f push numbers 10 through 15
    var value = instruction.charCodeAt(0) - 87;
    this.currStack.push(value);
  } else if (instruction == "$") {
    // Toggle the current stack
    if (this.currStack === this.ownStack) {
      this.currStack = this.program.sharedStack;
    } else {
      this.currStack = this.ownStack;
    }
  } else if (instruction == "s") {
    this.currStack = this.ownStack;
  } else if (instruction == "S") {
    this.currStack = this.program.sharedStack;
  } else if (instruction == "l") {
    this.currStack.push(this.ownStack.length);
  } else if (instruction == "L") {
    this.currStack.push(this.program.sharedStack.length);
  } else if (instruction == ".") {
    var item = this.currStack.pop();
    this.currStack.push(item);
    this.currStack.push(item);
  } else if (instruction == "m") {
    var item = this.ownStack.pop();
    this.program.sharedStack.push(item);
  } else if (instruction == "M") {
    var item = this.program.sharedStack.pop();
    this.ownStack.push(item);
  } else if (instruction == "y") {
    var item = this.ownStack.top();
    this.program.sharedStack.push(item);
  } else if (instruction == "Y") {
    var item = this.program.sharedStack.top();
    this.ownStack.push(item);
  } else if (instruction == "\\") {
    var top = this.currStack.pop();
    var next = this.currStack.pop()
    this.currStack.push(top);
    this.currStack.push(next);
  } else if (instruction == "@") {
    var c = this.currStack.pop();
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(c);
    this.currStack.push(a);
    this.currStack.push(b);
  } else if (instruction == ";") {
    this.currStack.pop();
  } else if (instruction == "+") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a + b);
  } else if (instruction == "-") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a - b);
  } else if (instruction == "*") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a * b);
  } else if (instruction == "/") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a / b);
  } else if (instruction == "%") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a % b);
  } else if (instruction == "_") {
    this.currStack.push(-this.currStack.pop());
  } else if (instruction == "I") {
    var value = this.currStack.pop();
    if (value < 0) {
      this.currStack.push(Math.ceil(value));
    } else {
      this.currStack.push(Math.floor(value));
    }
  } else if (instruction == ">") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a > b));
  } else if (instruction == "<") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a < b));
  } else if (instruction == "=") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a == b));
  } else if (instruction == "!") {
    this.currStack.push(+ !this.currStack.pop());
  } else if (instruction == "?") {
    this.currStack.push(Math.random());
  } else if (instruction == "n") {
    output = "" + this.currStack.pop();
  } else if (instruction == "o") {
    output = String.fromCharCode(this.currStack.pop());
  } else if (instruction == "r") {
    var input = this.program.io.getNumber();
    this.currStack.push(input);
  } else if (instruction == "i") {
    var input = this.program.io.getChar();
    this.currStack.push(input);
  } else if (instruction == "(") {
    this.length -= Math.floor(this.currStack.pop());
    this.length = Math.max(this.length, 0);
  } else if (instruction == ")") {
    this.length += Math.floor(this.currStack.pop());
    this.length = Math.min(this.length, this.code.length);
  } else if (instruction == "w") {
    this.wait = this.currStack.pop();
  }
  // Any unrecognized character is a no-op
  if (this.ip >= this.length) {
    // We've swallowed the IP, so this snake dies
    this.alive = false;
    this.program.snakesLiving--;
  } else {
    // Increment IP and loop if appropriate
    this.ip = (this.ip + 1) % this.length;
  }
  return output;
}
Snake.prototype.getHighlightedCode = function() {
  var result = "";
  for (var i = 0; i < this.code.length; i++) {
    if (i == this.length) {
      result += '<span class="swallowedCode">';
    }
    if (i == this.ip) {
      if (this.wait > 0) {
        result += '<span class="nextActiveToken">';
      } else {
        result += '<span class="activeToken">';
      }
      result += escapeEntities(this.code.charAt(i)) + '</span>';
    } else {
      result += escapeEntities(this.code.charAt(i));
    }
  }
  if (this.length < this.code.length) {
    result += '</span>';
  }
  return result;
}

// Define Program class
function Program(source, speed, io) {
  this.sharedStack = new Stack();
  this.snakes = source.split(/\r?\n/).map(function(snakeCode) {
    var snake = new Snake(snakeCode);
    snake.program = this;
    snake.sharedStack = this.sharedStack;
    return snake;
  }.bind(this));
  this.snakesLiving = this.snakes.length;
  this.io = io;
  this.speed = speed || 10;
  this.halting = false;
}
Program.prototype.run = function() {
  this.step();
  if (this.snakesLiving) {
    this.timeout = window.setTimeout(this.run.bind(this), 1000 / this.speed);
  }
}
Program.prototype.step = function() {
   for (var s = 0; s < this.snakes.length; s++) {
    var output = this.snakes[s].step();
    if (output) {
      this.io.print(output);
    }
  }
  this.io.displaySource(this.snakes.map(function (snake) {
      return snake.getHighlightedCode();
    }).join("<br>"));
 }
Program.prototype.halt = function() {
  window.clearTimeout(this.timeout);
}

var ioFunctions = {
  print: function (item) {
    var stdout = document.getElementById('stdout');
    stdout.value += "" + item;
  },
  getChar: function () {
    if (inputData) {
      var inputChar = inputData[0];
      inputData = inputData.slice(1);
      result = inputChar.charCodeAt(0);
    } else {
      result = -1;
    }
    var stdinDisplay = document.getElementById('stdin-display');
    stdinDisplay.innerHTML = escapeEntities(inputData);
    return result;
  },
  getNumber: function () {
    while (inputData && (inputData[0] < "0" || "9" < inputData[0])) {
      inputData = inputData.slice(1);
    }
    if (inputData) {
      var inputNumber = inputData.match(/\d+/)[0];
      inputData = inputData.slice(inputNumber.length);
      result = +inputNumber;
    } else {
      result = -1;
    }
    var stdinDisplay = document.getElementById('stdin-display');
    stdinDisplay.innerHTML = escapeEntities(inputData);
    return result;
  },
  displaySource: function (formattedCode) {
    var sourceDisplay = document.getElementById('source-display');
    sourceDisplay.innerHTML = formattedCode;
  }
};
var program = null;
var inputData = null;
function showEditor() {
  var source = document.getElementById('source'),
    sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
    stdin = document.getElementById('stdin'),
    stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
  
  source.style.display = "block";
  stdin.style.display = "block";
  sourceDisplayWrapper.style.display = "none";
  stdinDisplayWrapper.style.display = "none";
  
  source.focus();
}
function hideEditor() {
  var source = document.getElementById('source'),
    sourceDisplay = document.getElementById('source-display'),
    sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
    stdin = document.getElementById('stdin'),
    stdinDisplay = document.getElementById('stdin-display'),
    stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
  
  source.style.display = "none";
  stdin.style.display = "none";
  sourceDisplayWrapper.style.display = "block";
  stdinDisplayWrapper.style.display = "block";
  
  var sourceHeight = getComputedStyle(source).height,
    stdinHeight = getComputedStyle(stdin).height;
  sourceDisplayWrapper.style.minHeight = sourceHeight;
  sourceDisplayWrapper.style.maxHeight = sourceHeight;
  stdinDisplayWrapper.style.minHeight = stdinHeight;
  stdinDisplayWrapper.style.maxHeight = stdinHeight;
  sourceDisplay.textContent = source.value;
  stdinDisplay.textContent = stdin.value;
}
function escapeEntities(input) {
  return input.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
function resetProgram() {
  var stdout = document.getElementById('stdout');
  stdout.value = null;
  if (program !== null) {
    program.halt();
  }
  program = null;
  inputData = null;
  showEditor();
}
function initProgram() {
  var source = document.getElementById('source'),
    stepsPerSecond = document.getElementById('steps-per-second'),
    stdin = document.getElementById('stdin');
  program = new Program(source.value, +stepsPerSecond.innerHTML, ioFunctions);
  hideEditor();
  inputData = stdin.value;
}
function runBtnClick() {
  if (program === null || program.snakesLiving == 0) {
    resetProgram();
    initProgram();
  } else {
    program.halt();
    var stepsPerSecond = document.getElementById('steps-per-second');
    program.speed = +stepsPerSecond.innerHTML;
  }
  program.run();
}
function stepBtnClick() {
  if (program === null) {
    initProgram();
  } else {
    program.halt();
  }
  program.step();
}
function sourceDisplayClick() {
  resetProgram();
}
.container {
    width: 100%;
}
.so-box {
    font-family:'Helvetica Neue', Arial, sans-serif;
    font-weight: bold;
    color: #fff;
    text-align: center;
    padding: .3em .7em;
    font-size: 1em;
    line-height: 1.1;
    border: 1px solid #c47b07;
    -webkit-box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
    background: #f88912;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
}
.control {
    display: inline-block;
    border-radius: 6px;
    float: left;
    margin-right: 25px;
    cursor: pointer;
}
.option {
    padding: 10px 20px;
    margin-right: 25px;
    float: left;
}
h1 {
    text-align: center;
    font-family: Georgia, 'Times New Roman', serif;
}
a {
    text-decoration: none;
}
input, textarea {
    box-sizing: border-box;
}
textarea {
    display: block;
    white-space: pre;
    overflow: auto;
    height: 50px;
    width: 100%;
    max-width: 100%;
    min-height: 25px;
}
span[contenteditable] {
    padding: 2px 6px;
    background: #cc7801;
    color: #fff;
}
#stdout-container, #stdin-container {
    height: auto;
    padding: 6px 0;
}
#reset {
    float: right;
}
#source-display-wrapper , #stdin-display-wrapper{
    display: none;
    width: 100%;
    height: 100%;
    overflow: auto;
    border: 1px solid black;
    box-sizing: border-box;
}
#source-display , #stdin-display{
    font-family: monospace;
    white-space: pre;
    padding: 2px;
}
.activeToken {
    background: #f93;
}
.nextActiveToken {
    background: #bbb;
}
.swallowedCode{
    color: #999;
}
.clearfix:after {
    content:".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
.clearfix {
    display: inline-block;
}
* html .clearfix {
    height: 1%;
}
.clearfix {
    display: block;
}
<!--
Designed and written 2015 by D. Loscutoff
Much of the HTML and CSS was taken from this Befunge interpreter by Ingo Bürk: http://codegolf.stackexchange.com/a/40331/16766
-->
<div class="container">
<textarea id="source" placeholder="Enter your program here" wrap="off">i.0<5*(o32o</textarea>
<div id="source-display-wrapper" onclick="sourceDisplayClick()"><div id="source-display"></div></div></div><div id="stdin-container" class="container">
<textarea id="stdin" placeholder="Input" wrap="off">Input text...</textarea>
<div id="stdin-display-wrapper" onclick="stdinDisplayClick()"><div id="stdin-display"></div></div></div><div id="controls-container" class="container clearfix"><input type="button" id="run" class="control so-box" value="Run" onclick="runBtnClick()" /><input type="button" id="pause" class="control so-box" value="Pause" onclick="program.halt()" /><input type="button" id="step" class="control so-box" value="Step" onclick="stepBtnClick()" /><input type="button" id="reset" class="control so-box" value="Reset" onclick="resetProgram()" /></div><div id="stdout-container" class="container"><textarea id="stdout" placeholder="Output" wrap="off" readonly></textarea></div><div id="options-container" class="container"><div class="option so-box">Steps per Second:
<span id="steps-per-second" contenteditable>20</span></div></div>

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

Pyth, 5 bytes

+jdzd

Test here

Explanation:

 jdz    Join input with spaces
+   d   Concatenate space
\$\endgroup\$
1
\$\begingroup\$

0815, 26 bytes

<:20:~>}: :!~$#:
:{~$>^: :
\$\endgroup\$
1
\$\begingroup\$

Silicon, 7 bytes

Note that this is non-competing. How I wish that I would have made Silicon last year...

iSæ²jæ+

Explanation:

i        Input
S        Split the input
æ        Push a space
²j       Join at spaces
æ+       Append a space to the end of the string
         Implicit output
\$\endgroup\$
  • 1
    \$\begingroup\$ Does Silicon have it's own encoding? Else, it would be 10 bytes in UTF-8 (and 7 chars). \$\endgroup\$ – Katenkyo Jul 6 '16 at 9:07
1
\$\begingroup\$

Tcl, 44 bytes

puts [regsub -all {(.)} [gets stdin] {\1 }]
\$\endgroup\$
  • \$\begingroup\$ You're byte count is wrong! It is 43! \$\endgroup\$ – sergiol Jan 12 '17 at 3:22
1
\$\begingroup\$

Lua, 31 Bytes

Saved 1 byte thanks to @LeakyNun

A simple chained substitution, replace each character by itself and a space.

s=(...):gsub(".","%1 ")print(s)

An other solution with the same byte count:

print((...):gsub(".","%1 "),'')
\$\endgroup\$
  • \$\begingroup\$ @manatwork I don't see how I did to miss this! Print automatically had a tab between each of it's parameters, totally forgot that point. Thanks for the comment! \$\endgroup\$ – Katenkyo Apr 4 '16 at 7:41
  • \$\begingroup\$ Can you change arg[1] to ...? \$\endgroup\$ – Leaky Nun Jul 1 '16 at 7:57
1
\$\begingroup\$

Swift 2.2, 48 46 bytes

for c in i.characters{print(c,terminator:" ")}

Hey, at least it's shorter than Java... Saved 2 chars by using a for in loop instead of a forEach closure.

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

Batch, 87 bytes

@set/pt=&set w=
:l
@if not "%t%"=="" set w=%w%%t:~,1% &set t=%t:~1%&goto l
@echo(%w%

Conveniently taking input from STDIN is the same length as taking it from program arguments (set t=%*). The space between the % and & on the third line is significant, of course. The ( after the echo simply prevents it from erroring out if the input is empty.

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

tcl, 41

puts [regsub -all (.) [gets stdin] \\1\ ]

on http://rextester.com/KCIB63188

\$\endgroup\$
  • \$\begingroup\$ Just noticed now, there was another Tcl answer! \$\endgroup\$ – sergiol Jan 12 '17 at 3:20
1
\$\begingroup\$

TI-84 BASIC, 44 bytes

Pretty straightforward approach:

Input Str1
sub(Str1,1,1
For(A,2,length(Str1
Ans+" "+sub(Str1,A,1
End
Ans+" 

The only size optimizations I could come up with were:

  • leaving off closing quotes and parentheses at the end of lines
  • using Ans instead of a string variable (the Ans token is 1 byte, StrN are 2, and assignment to Ans is implicit)
  • printing the result by placing it on the last line of the program (no Disp command needed)

This program wouldn't work correctly for an empty input string, since it unconditionally stores the first character of Str1 in Ans. (This is necessary because concatenation with an empty string raises an error!) Fortunately, the Input command doesn't accept an empty string in the first place.

The size calculation was informed by TI-Basic Developer's token size charts.

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

OIL, 52 49 bytes noncompeting

Note: The second line contains a space character.

5

12
0
23
10
24
3
7
10
4
24
4
1
8
11
1
11
6
6
5

I explode the input string, then print each character and then a space.

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

Pushy, 7 bytes

Note that this answer is non-competing as the language postdates the challenge.

32DL:{'

Try it online!

Works like so:

       \ Implicit: Input is on stack as charcodes
32D    \ Set the printing delimiter to a space
L:     \ Input length times do:
  {    \   Cyclically shift the stack left
   '   \   Print the last character (includes trailing space)

Alternatively, for the same byte count, one could have:

L:{32;"

Try it online!

L:   ;   \ Length times do:
  {      \   Shift string left
   32    \   Insert char 32 (a space)
      "  \ Afterwards, print the whole string
\$\endgroup\$
1
\$\begingroup\$

Japt, 4 bytes

U®+S

Non-competing 3-byte answer:

®+S

Try it online!

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

Lua, 57 bytes

t=""io.read():gsub(".",function(c)t=t..c.." "end)print(t)
\$\endgroup\$
1
\$\begingroup\$

SmileBASIC 3, 44 bytes

INPUT A$FOR I=0TO LEN(A$)-1?A$[I]+" ";
NEXT

This doesn't leave a new line after the output, so from direct you might get

?HELLO
H E L L O OK

but hopefully it's not an issue.

\$\endgroup\$
  • \$\begingroup\$ You can make the loop shorter by doing: WHILE""<A$?SHIFT(A$);" ";:WEND \$\endgroup\$ – 12Me21 Apr 21 '17 at 14:17
1
\$\begingroup\$

q/kdb+, 12 bytes

Solution:

raze{x,'" "}

Example:

q)raze{x,'" "}"Hello World!"
"H e l l o   W o r l d ! "

Explanation:

,'   / join (,) each left ("H", "e", "l"...) with each right (" ")
raze / reduce list

Notes:

Would be 9 bytes doing it more directly rather than as a lambda function:

q)raze"Hello World!",'" "
"H e l l o   W o r l d ! " 
\$\endgroup\$
1
\$\begingroup\$

Javascript, 16 bytes

[...s,,].join` `
\$\endgroup\$
  • 1
    \$\begingroup\$ Unfortunately, this is a snippet (s isn't ever defined), but to make it a valid submission you can add s=> to the start. \$\endgroup\$ – caird coinheringaahing Dec 14 '17 at 16:07
1
\$\begingroup\$

Vim, 12 10 keystrokes

:s/./& /g<CR>

1 byte longer:

qqa <Esc>l@qq@q

-2 bytes thanks to @KritixiLithos

\$\endgroup\$
  • \$\begingroup\$ You don't need to delete the last space and :s/./\0 /g<CR>$x can become just :s/./& /g<CR> at 10 bytes. \$\endgroup\$ – Kritixi Lithos Jun 5 '17 at 4:05
1
\$\begingroup\$

Brain-Flak, 46 bytes

{({}<>)<>}<>{({}<>)((((()()()()){}){}){})<>}<>

Try it online!

Explanation:

#Reverse the whole stack
{({}<>)<>}<>

#Reverse the whole stack, 
{({}<>)

    #Pushing 32 between each character
    ((((()()()()){}){}){})<>}<>
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog), 4 bytes

Requires ⎕ML←0 which used to be standard

,⍪,∊

Try it online!

, ravel (flatten)

 the columnified argument

, followed by

 the prototypes (one space per character)

\$\endgroup\$
  • \$\begingroup\$ Can you explain why this needs to be encoded in UTF-8? \$\endgroup\$ – Zacharý Jul 26 '17 at 16:01
  • \$\begingroup\$ @Zacharý Does it? Is ⎕AVU no good? \$\endgroup\$ – Adám Jul 26 '17 at 16:03
  • \$\begingroup\$ Clarification: Why this is 12 bytes and not 4? \$\endgroup\$ – Zacharý Jul 26 '17 at 16:06
  • \$\begingroup\$ @Zacharý Ah, simple mistake. \$\endgroup\$ – Adám Jul 26 '17 at 16:30
1
\$\begingroup\$

APL, 11 bytes

{,/' ',¨⍵}

catenate ' ' with each in omega (argument), then join (reduce-catenate, ,/)

\$\endgroup\$
  • \$\begingroup\$ You don't need the anymore. 10 bytes: {,/⍵,¨' '} \$\endgroup\$ – Zacharý Jul 26 '17 at 17:28
  • \$\begingroup\$ @Zacharý "anymore"? \$\endgroup\$ – Ven Jul 26 '17 at 18:24
  • \$\begingroup\$ I thought you originally had it to save bytes, guess I was wrong. Either way, you don't need . \$\endgroup\$ – Zacharý Jul 26 '17 at 18:29
1
\$\begingroup\$

Julia, 31 bytes

print(join(readline(),' ')*" ")

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Rɪᴋᴇʀ Dec 17 '17 at 17:43
1
\$\begingroup\$

C#, 52 bytes

Manual, straightforward approach, without using Linq.

s=>{var r="";foreach(var c in s)r+=c+" ";return r;};

Anonymous method which gets every character from the string and adds a space after each one.

Full program:

using System;

namespace FullWidthText
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string> f =
            s =>
            {
                var r = "";
                foreach (var c in s)
                    r += c + " ";
                return r;
            };

            Console.WriteLine(f("Full width text is text that has a space after every character, including the last one."));
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

C 55, 49 bytes

g(){c;while((c=getchar())!='\n')printf("%c ",c);}

Ungolfed version:

void g()
{
  char c;
  while((c=getchar())!='\n')
  printf("%c ", c);
}
\$\endgroup\$

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.