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>
-
8\$\begingroup\$ Usually you should allow functions too, or you exclude a lot of languages (e.g. JavaScript). \$\endgroup\$wizzwizz4– wizzwizz42016年03月22日 18:00:20 +00:00Commented Mar 22, 2016 at 18:00
-
10\$\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– Dennis2016年03月22日 21:22:58 +00:00Commented Mar 22, 2016 at 21:22
-
65\$\begingroup\$ That is not what fullwidth text is. \$\endgroup\$BlueRaja - Danny Pflughoeft– BlueRaja - Danny Pflughoeft2016年03月22日 21:53:15 +00:00Commented Mar 22, 2016 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– Ludovic Frérot2016年03月23日 09:27:15 +00:00Commented Mar 23, 2016 at 9:27
-
3\$\begingroup\$ @LudovicFrérot Actually, these are not ideograms, these are chinese english letters. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年07月01日 08:29:17 +00:00Commented Jul 1, 2016 at 8:29
151 Answers 151
Brain-Flak, 46 bytes
{({}<>)<>}<>{({}<>)((((()()()()){}){}){})<>}<>
Explanation:
#Reverse the whole stack
{({}<>)<>}<>
#Reverse the whole stack,
{({}<>)
#Pushing 32 between each character
((((()()()()){}){}){})<>}<>
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."));
}
}
}
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);
}
C#, 35 bytes
s=>string.Join(" ",s.ToList())+" ";
C# lambda where the input and the output are string. You can try it on .NetFiddle.
-
\$\begingroup\$ This requires a
using System.Linqderivative, which should either be included or the language marked as LinqPad or similar. \$\endgroup\$VisualMelon– VisualMelon2017年01月12日 11:07:07 +00:00Commented Jan 12, 2017 at 11:07
Python 3, 39 bytes
print("".join([x+" "for x in input()]))
-
1\$\begingroup\$ You could just join with a
" "rather than insert it with the comprehension. Then all you need to worry about is the trailing space at the end.print(" ".join(input())+" ")gets you down to 28 bytes \$\endgroup\$wnnmaw– wnnmaw2016年03月23日 16:15:58 +00:00Commented Mar 23, 2016 at 16:15 -
-
\$\begingroup\$ @manatwork I see. Should I change my old answer back or something? Thanks for telling! \$\endgroup\$Yytsi– Yytsi2016年03月24日 11:37:16 +00:00Commented Mar 24, 2016 at 11:37
-
1\$\begingroup\$ Just improve it more. ;) In meantime I would revert to the previous version – it demonstrates the usage of a very popular Python feature, which has better chances for positive votes than a duplicate. \$\endgroup\$manatwork– manatwork2016年03月24日 12:17:44 +00:00Commented Mar 24, 2016 at 12:17
-
\$\begingroup\$ @manatwork Very good point! Reverted it. Back to 39 bytes :D \$\endgroup\$Yytsi– Yytsi2016年03月24日 12:32:03 +00:00Commented Mar 24, 2016 at 12:32
Python 3, (削除) 31 (削除ここまで) 28
print(' '.join(input())+' ')
I know i'm late on this one, and definitely not a winner, but thought i'd try it out.
Edit: Switched to python 3. Thanks to CatsAreFluffy for saving 3 bytes!
-
\$\begingroup\$ You can cut one byte by removing the space after
print. \$\endgroup\$Zach Gates– Zach Gates2016年03月22日 22:53:06 +00:00Commented Mar 22, 2016 at 22:53 -
\$\begingroup\$ Or just switch to Python 3 (and cut 3 bytes):
print(' '.join(input)+' ').raw_inputis more expensive than parens withprint. \$\endgroup\$CalculatorFeline– CalculatorFeline2016年03月23日 02:31:26 +00:00Commented Mar 23, 2016 at 2:31
Python 3, (削除) 95 93 92 80 32 26 (削除ここまで) 30 bytes
Saved a byte thanks to @caird coinheringaahing
Saved bunch of bytes from @DJMcMayhem + My idea
print(' '.join(input())+' ')
Takes input, splits it, then prints a join with spaces
This is my first time golfing.
-
1\$\begingroup\$ Welcome to the site! You can remove a byte by removing the space after the
:as well as taking a look at these tips for Python golfing \$\endgroup\$2018年01月26日 16:26:51 +00:00Commented Jan 26, 2018 at 16:26 -
\$\begingroup\$ You could get this down to a one-liner:
print(''.join(i+' 'for i in input())\$\endgroup\$DJMcMayhem– DJMcMayhem2018年01月26日 16:41:19 +00:00Commented Jan 26, 2018 at 16:41 -
1\$\begingroup\$ @DJMcMayhem, made it even shorter :) \$\endgroup\$Mercury Platinum– Mercury Platinum2018年01月26日 16:45:15 +00:00Commented Jan 26, 2018 at 16:45
-
\$\begingroup\$ Does this print a space after the last character? And do you need the
list()part? \$\endgroup\$Jo King– Jo King2018年01月27日 05:36:43 +00:00Commented Jan 27, 2018 at 5:36 -
\$\begingroup\$ oh, one second. \$\endgroup\$Mercury Platinum– Mercury Platinum2018年01月27日 16:36:51 +00:00Commented Jan 27, 2018 at 16:36
Common Lisp, 40 bytes
(map()(lambda(x)(format t"~C "x))(read))
I was very surprised that format couldn't do this by itself: it can only iterate over lists, not strings.
Canvas, (削除) 4 (削除ここまで) 3 bytes
]∑
Explanation:
Push input to stack (implicit)
For each character in the input: (implicit)
Push the character to the stack (implicit)
Push " " to the stack
] Collapse results from loop into an array
∑ Join the array into a single string
Print the ToS (implicit)
-1 byte thanks to dzaima!
-
1\$\begingroup\$ You can remove the leading
{as it can be implicit for 3 bytes \$\endgroup\$dzaima– dzaima2018年03月06日 16:23:22 +00:00Commented Mar 6, 2018 at 16:23
SMBF, 9 bytes
Note the trailing space. Uses the space in the code rather than creating a 32 in a cell.
,[.<.>,]
The tryitonline.net interpreter appears to have a bug and loop indefinitely with this, so it only works in my Python interpreter. Changing the SMBF source code (currently on line 171) inside the Python code is required.
Brainfuck, 23 (削除) 24 (削除ここまで) (削除) 25 (削除ここまで) Bytes
1 Byte thanks to ovs
-[-[-<]>>+<]>-<+[,.>.<]
Try it online!
This code makes use of most interpreters wrapping to 255 if the number is negative.
Previous solution:
++++[>++++++++<-]+[,.>.<]
Explanation:
++++[>++++++++<-] |Leaves the second cell with the number 32, the ASCII Code for a space.
+[,.>.<] |Simple loop, gets the input, outputs it and adds a space.
Suggestions are welcome.
-
-
\$\begingroup\$ Cool, very nice. I have problems understanding your program though. Could you explain it to me? \$\endgroup\$Dust– Dust2018年01月27日 10:21:55 +00:00Commented Jan 27, 2018 at 10:21
-
\$\begingroup\$ The code makes use of the fact that most brainfuck implementations use wrapping 8-bit cells. If you decrement a cell that is zero, you get to
255. Here is a simple example. \$\endgroup\$ovs– ovs2018年01月27日 10:35:10 +00:00Commented Jan 27, 2018 at 10:35 -
\$\begingroup\$ The
,should be at the end of the loop, otherwise you print an extra nul byte. Try it online! \$\endgroup\$Jo King– Jo King2019年02月12日 00:47:32 +00:00Commented Feb 12, 2019 at 0:47 -
Gol><>, 7 bytes
iEH}` }
Courtesy of JoKing, who golfed this to the max!!! JoKing combined the loops into one simple loop, also avoided the mistake I made, I added 2 spaces rather than one.
Slightly newer of My original golfed, 23 bytes
Ti:0)?t~l&T` }}&M:&?trH
Old version, 28 bytes
Ti:0)?t~l&T" "}}}&M:&0)?trH
This can probably be golfed futher, but code breakdown below!
Ti:0)?t~l&T" "}}}&M:&0)?trH
Ti:0)?t~ //gets every char from input, and pushes them to the stack
T" "}}} //push 2 spaces after every char, push to the bottom of the stack for later
l&T &M:&0)?t //push original length and store, every loop decrement and if it is greater than zero, repeat
rH//reverse stack and output the whole stack as characters
-
-
\$\begingroup\$ @JoKing Wow, that is really smart using the E function! Also I just realized that it only needs one space, and that I forgot to add spaces after the last characters, sorry! \$\endgroup\$KrystosTheOverlord– KrystosTheOverlord2019年02月12日 00:31:35 +00:00Commented Feb 12, 2019 at 0:31
-
\$\begingroup\$ @JoKing Almost managed 6 bytes, but it prints entirely backwards... iEH@` \$\endgroup\$KrystosTheOverlord– KrystosTheOverlord2019年02月12日 00:44:26 +00:00Commented Feb 12, 2019 at 0:44
Zsh, 21 bytes
echo ${(s::)$(<&0)}\
$(capture), ${(s::)plit}, echo. The last "\ " is needed for the trailing space.
Befunge-98 (PyFunge), 6 bytes
"#@~,,
The Befunge-98 answer is pretty long, so here is a shorter one. Basically the same answer as this Befunge-93 answer by Sp3000, but ends up being half as long, thanks to Befunge-98s ~ reflecting on EOF.
One thing that bugged me is, that when you run it on multi line input, every line but the first will be indented (since the linebreak will be followed by a space as well), so here is a version (11 bytes), that doesn't have that problem.
"#@~:,a-!j,
Keg, -rs, 3 bytes
( '
Shifts the input putting a space after each character, reverses the stack via the -rs flag and then implicitly outputs the stack.
PowerShell, (削除) 17 (削除ここまで) 8 bytes
-9 bytes thanks to @mazzy!
"$args "
There are already a couple of PowerShell answers to this question, but I thought I'd throw mine into the mix. It takes splatter input from a command line argument. It also assumes that $ofs is equal to one space, which it is by default.
-
1
-
\$\begingroup\$ @mazzy finally, a competitive PowerShell answer ;) thanks! \$\endgroup\$GMills– GMills2020年01月02日 16:37:31 +00:00Commented Jan 2, 2020 at 16:37
Vyxal Ṫ, (削除) 3 (削除ここまで) 2 bytes
Ṅð
Explanation:
# Implicit input
Ṅ # Insert ' ' between each character
ð # Push ' '
# 'Ṫ' flag - sum entire stack and print
If the final space wasn't necessary:
Vyxal S, 0 bytes
Explanation:
# Implicit input
# 'S' flag - Print top of stack joined by spaces
-
\$\begingroup\$
ð*withsalso works for 2 bytes \$\endgroup\$emanresu A– emanresu A2021年09月23日 19:54:20 +00:00Commented Sep 23, 2021 at 19:54
Silicon, 6 bytes
iSæ%æ+
Explanation:
iSæ%æ+
i Input
S Split the input
æ Push a space
% Join at spaces
æ+ Append a space to the end of the string
Implicit output
(削除) 7 bytes (削除ここまで)
iSæ2jæ+
-
1\$\begingroup\$ Does Silicon have it's own encoding? Else, it would be 10 bytes in UTF-8 (and 7 chars). \$\endgroup\$Katenkyo– Katenkyo2016年07月06日 09:07:00 +00:00Commented Jul 6, 2016 at 9:07
-
\$\begingroup\$ @katenkyo I think it has. \$\endgroup\$user100411– user1004112021年09月25日 00:21:49 +00:00Commented Sep 25, 2021 at 0:21
Factor + sequences.interleaved, 29 bytes
readln 32 <interleaved> print
Modern versions of Factor (i.e. not TIO) allow most sequences to be printed as strings. So have a screenshot of running this in the listener:
Javascript, 19 bytes
s=>[...s,,].join` `
Pushy, 7 bytes
32DL:{'
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;"
L: ; \ Length times do:
{ \ Shift string left
32 \ Insert char 32 (a space)
" \ Afterwards, print the whole string
Commodore C64 BASIC, 39 Tokenised BASIC bytes, 42 PETSCII characters with keyword abbreviations
0INPUTA$:FORI=1TOLEN(A$):?MID$(A,ドルI,1)" ";:NEXT
On running this listing, the interpreter will expect an input of strings which when the return key is pressed, will store the text entered into the variable A$. It will then iterate over each position of the string to its length and PRINT it, proceeded by a white space, and then will continue at the next cursor position until the final character of the string is printed (this will be followed by a space too).
Some notes: A Commodore C64 screen is 40 characters wide by default, and the maximum number of characters that may be stored in a string is 255, however, when using INPUT the interpreter will only accept a total entry of 77 characters without some trickery (80 characters if you delete the ? prompt, enter 80 characters and then cursor up two lines and press the RETURN key).
See screenshot for this in action.
Pascal, 90 bytes
This is a full program suitable for a processor supporting "Standard Pascal" and/or "Extended Pascal" as defined by ISO standards 7185 and 10206 respectively.
As per specification it takes a line (not many lines).
Note, calling EOLn(input) causes an error if EOF(input) is true prior invocation of EOLn.
Hence it must be guaranteed that input contains at least an end‐of‐line marker.
program p(input,output);begin while not EOLn do begin write(input↑,' ');get(input)end end.
We can add −10 bytes if it is guaranteed that input is a non‐empty line:
program p(input,output);begin repeat write(input↑,' ');get(input)until EOLn end.
Dyalog APL, (削除) 11 (削除ここまで) 9 bytes
,/,∘' ' ̈
Train that each-adds a space, then joins
-
\$\begingroup\$ You don't need the
⍨anymore. 10 bytes:{,/⍵,¨' '}\$\endgroup\$Adalynn– Adalynn2017年07月26日 17:28:30 +00:00Commented Jul 26, 2017 at 17:28 -
\$\begingroup\$ @Zacharý "anymore"? \$\endgroup\$Ven– Ven2017年07月26日 18:24:37 +00:00Commented Jul 26, 2017 at 18:24
-
\$\begingroup\$ I thought you originally had it to save bytes, guess I was wrong. Either way, you don't need
⍨. \$\endgroup\$Adalynn– Adalynn2017年07月26日 18:29:54 +00:00Commented Jul 26, 2017 at 18:29
Tcl, 37 bytes
puts [join [split [gets stdin] ""]]\
puts [regsub -all (.) [gets stdin] \1円\ ]
(削除ここまで)
-
\$\begingroup\$ Just noticed now, there was another Tcl answer! \$\endgroup\$sergiol– sergiol2017年01月12日 03:20:47 +00:00Commented Jan 12, 2017 at 3:20
Javascript, 35 chars
Full program
alert(prompt().replace(/./g,"$& "))
Javascript ES6, 24 chars
Just a function
s=>s.replace(/./g,"$& ")
-
\$\begingroup\$ As we are using a leaderboard right now, would you mind modifying your title for just "Javascript", as it will actually be considered as an other language. \$\endgroup\$Katenkyo– Katenkyo2016年03月23日 09:50:35 +00:00Commented Mar 23, 2016 at 9:50
-
\$\begingroup\$ @Katenkyo, updated \$\endgroup\$Qwertiy– Qwertiy2016年03月23日 09:54:26 +00:00Commented Mar 23, 2016 at 9:54
Befunge-98, 21 bytes
> ~:av
^,*84,w
@,*84,<
Lengthier than one already present but has the ending space requested by OP.