65
\$\begingroup\$

Super simple challenge today, or is it?

I feel like we've heard a fair bit about double speak recently, well let's define it in a codable way...

Double speak is when each and every character in a string of text is immediately repeated. For example:

"DDoouubbllee  ssppeeaakk!!"

The Rules

  • Write code which accepts one argument, a string.
  • It will modify this string, duplicating every character.
  • Then it will return the double speak version of the string.
  • It's code golf, try to achieve this in the smallest number of bytes.
  • Please include a link to an online interpreter for your code.
  • Input strings will only contain characters in the printable ASCII range. Reference: http://www.asciitable.com/mobile/

Leaderboards

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

var QUESTION_ID=188988;
var OVERRIDE_USER=53748;
var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(d){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+d+"&pagesize=100&order=asc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(d,e){return"https://api.stackexchange.com/2.2/answers/"+e.join(";")+"/comments?page="+d+"&pagesize=100&order=asc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(d){answers.push.apply(answers,d.items),answers_hash=[],answer_ids=[],d.items.forEach(function(e){e.comments=[];var f=+e.share_link.match(/\d+/);answer_ids.push(f),answers_hash[f]=e}),d.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(d){d.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),d.has_more?getComments():more_answers?getAnswers():process()}})}getAnswers();var SCORE_REG=function(){var d=String.raw`h\d`,e=String.raw`\-?\d+\.?\d*`,f=String.raw`[^\n<>]*`,g=String.raw`<s>${f}</s>|<strike>${f}</strike>|<del>${f}</del>`,h=String.raw`[^\n\d<>]*`,j=String.raw`<[^\n<>]+>`;return new RegExp(String.raw`<${d}>`+String.raw`\s*([^\n,]*[^\s,]),.*?`+String.raw`(${e})`+String.raw`(?=`+String.raw`${h}`+String.raw`(?:(?:${g}|${j})${h})*`+String.raw`</${d}>`+String.raw`)`)}(),OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(d){return d.owner.display_name}function process(){var d=[];answers.forEach(function(n){var o=n.body;n.comments.forEach(function(q){OVERRIDE_REG.test(q.body)&&(o="<h1>"+q.body.replace(OVERRIDE_REG,"")+"</h1>")});var p=o.match(SCORE_REG);p&&d.push({user:getAuthorName(n),size:+p[2],language:p[1],link:n.share_link})}),d.sort(function(n,o){var p=n.size,q=o.size;return p-q});var e={},f=1,g=null,h=1;d.forEach(function(n){n.size!=g&&(h=f),g=n.size,++f;var o=jQuery("#answer-template").html();o=o.replace("{{PLACE}}",h+".").replace("{{NAME}}",n.user).replace("{{LANGUAGE}}",n.language).replace("{{SIZE}}",n.size).replace("{{LINK}}",n.link),o=jQuery(o),jQuery("#answers").append(o);var p=n.language;p=jQuery("<i>"+n.language+"</i>").text().toLowerCase(),e[p]=e[p]||{lang:n.language,user:n.user,size:n.size,link:n.link,uniq:p}});var j=[];for(var k in e)e.hasOwnProperty(k)&&j.push(e[k]);j.sort(function(n,o){return n.uniq>o.uniq?1:n.uniq<o.uniq?-1:0});for(var l=0;l<j.length;++l){var m=jQuery("#language-template").html(),k=j[l];m=m.replace("{{LANGUAGE}}",k.lang).replace("{{NAME}}",k.user).replace("{{SIZE}}",k.size).replace("{{LINK}}",k.link),m=jQuery(m),jQuery("#languages").append(m)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;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="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <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><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><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, [Other information] N bytes

where N is the size of your submission. Other information may include flags set and if you've improved your score (usually a struck out number like <s>M</s>). N should be the right-most number in this heading, and everything before the first , is the name of the language you've used. The language name and the word bytes may be links.

For example:

# [><>](http://esolangs.org/wiki/Fish), <s>162</s> 121 [bytes](https://esolangs.org/wiki/Fish#Instructions)
\$\endgroup\$
  • 1
    \$\begingroup\$ It will modify this string. Are you intentionally requiring pass-by-reference and modify in-place? And then return a copy or reference to that modified string? If so, languages like asm or C would need to accept an explicit-length string (pointer + length) where the length is either the current string length (with the buffer being twice that size), or it's the total size and you need to duplicate the low half. Thus you need to start from the end and work backwards, or allocate scratch space and then copy back. But there are answers in C and 8086 asm that totally violate all that. \$\endgroup\$ – Peter Cordes Aug 3 '19 at 1:49
  • 3
    \$\begingroup\$ @PeterCordes I do not care if it modifies the same object or builds a new one. \$\endgroup\$ – AJFaraday Aug 3 '19 at 5:08
  • 2
    \$\begingroup\$ I'd suggest wording it as "modify (or produce a modified copy) of the string" to explicitly allow answers that do or don't modify in-place. Simplifying the wording to "return a string that's twice as long, with each character repeated" would be nice but then it's not clear if void foo(char *c, size_t len) is legal that takes one input/output buffer and a length, and doesn't have any return value, just a side-effect on the object it has a pointer to. \$\endgroup\$ – Peter Cordes Aug 3 '19 at 5:16
  • \$\begingroup\$ Can the string be empty? \$\endgroup\$ – cschultz2048 Aug 6 '19 at 19:58
  • 1
    \$\begingroup\$ @cschultz2048 it says the string will only contain printable ascii characters, so that implies that they’ll always be populated. I’d expect that any code for this challenge would leave an empty string empty... anyway, I don’t think it’s a test case that I’d use for this. \$\endgroup\$ – AJFaraday Aug 6 '19 at 22:36

147 Answers 147

3
\$\begingroup\$

Gaia, 4 3 bytes

:¦$

Basically just maps the duplicate instruction to the input string, then turns the returned list into a string

Edit: turns out there was already a 1 byte answer in Gaia for this question that I missed, so my solution isn't that great

How it works

:   Duplicate
¦   Map to each character of the input string
$   Convert the returned list to a string
Auto outputs stack

Try it Online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You don't need @ because the interpreter will implicitly push values onto the stack if needed, which is why Z works just fine. \$\endgroup\$ – Giuseppe Aug 13 '19 at 10:57
  • \$\begingroup\$ Oh wow, I didn't realise that, my solution is really sub-optimal \$\endgroup\$ – EdgyNerd Aug 13 '19 at 12:45
  • \$\begingroup\$ Well, I've been golfing in Gaia a bit; this is still quite good! \$\endgroup\$ – Giuseppe Aug 13 '19 at 12:56
  • \$\begingroup\$ there's also 2&. \$\endgroup\$ – Giuseppe Aug 13 '19 at 17:12
  • \$\begingroup\$ reading through the docs for Gaia is annoying, especially since the interpreter is rather buggy. \$\endgroup\$ – Giuseppe Aug 13 '19 at 17:12
3
\$\begingroup\$

Pepe, 19 bytes

REEerEErReEeReEeree

Try it online!

Explanation:

REEe # Input as str -> (R)
rEE # Create loop labelled 0 -> (r)
  rReEe # Output as char -> (R)
        # r flag: don't pop
  ReEe # Output as char and pop -> (R)
ree # Loop while (R) != 0
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

C (gcc), 57 56 bytes

Thanks to ceilingcat and gastropher for the suggestions.

The string is modified in-place, so the buffer holding the string needs to be big enough first.

i;f(char*s){for(i=strlen(s)+1;i--;s[i-~i]=s[i*2]=s[i]);}

Try it online!

C version with allocated string, 73 71 bytes

If the string buffer can't be assumed to be big enough (or writable), this version allocates a buffer to return.

f(s,t,u)char*s,*t;{for(u=t=malloc(strlen(s)*3);*t++=*s;*t++=*s++);s=u;}

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Far as I could tell, there is no rule against wastefulness, so you could do malloc(strlen(s)*3) in the allocated string version to save two bytes. \$\endgroup\$ – gastropner Aug 20 '19 at 2:42
  • \$\begingroup\$ You can save 13 bytes by using recursion in your in place modified version: x;f(char*s){x++;*s&&f(s+1);s[--x]=s[x]=*s;} \$\endgroup\$ – xibu Dec 1 '19 at 19:39
3
\$\begingroup\$

Wolfram Language (Mathematica), 13 11 bytes

-2 thanks to LegionMammal978

#~Riffle~#&

Try it online!

Takes and returns an array of characters.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I believe the more straightforward #~Riffle~#& would be a valid 11-byte solution (taking and returning a character list). \$\endgroup\$ – LegionMammal978 Nov 3 '19 at 14:51
3
\$\begingroup\$

Poetic, 46 49 bytes

+3 bytes to account for inputs that are longer than the tape will allow (unlikely, but possible).

enduring a tragedy,knowing a way i am,troubles me

Try it online!

Poetic is an esolang I created in 2018 for a class project. It's basically brainfuck with word-lengths instead of symbols.

The point of the language is to allow for programs to be written in free-verse poetry. I encourage you all to try it out sometime!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Taxi, 390 342 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.[A]Go to Chop Suey:n 1 r 1 l 4 r 1 l.Switch to plan B waiting.Pickup a passenger going to Cyclone.Go to Zoom Zoom:n 1 l 3 r.Go to Cyclone:w.Pickup a passenger going to Post Office.Pickup a passenger going to Post Office.Go to Post Office:s 1 l 2 r 1 l.Switch to plan A.[B]

Try it online!

Explanation/Ungolfed

    [Take input string]
Go to Post Office: west 1st left, 1st right, 1st left.
Pickup a passenger going to Chop Suey.

[loop]
    [If string as a passenger, split it]
Go to Chop Suey: north 1st right, 1st left, 4th right, 1st left.
    [At the end of the string, end the program]
Switch to plan end_loop if noone is waiting.
        ["if noone is" apparently optional here]
    [Duplicate first char left]
Pickup a passenger going to Cyclone.
Go to Zoom Zoom: north 1st left, 3rd right.
Go to Cyclone: west.
    [Print char twice]
Pickup a passenger going to the Post Office.
Pickup another passenger going to the Post Office.
Go to the Post Office: south 1st left, 2nd right, 1st left.
Switch to plan loop.

[end_loop]
    [Program crashes, Taxi not in Garage at end of the week]

Comment

It does not even matter whether we tank at Zoom Zoom or Fueler Up, both were 390 bytes and now are 342 bytes:

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.[A]Go to Fueler Up:n 1 r 1 l.Go to Chop Suey:n 3 r 1 l.Switch to plan B waiting.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 3 l.Pickup a passenger going to Post Office.Pickup a passenger going to Post Office.Go to Post Office:s 1 l 2 r 1 l.Switch to plan A.[B]

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can save four bytes by removing the quotation marks in the "Switch to plan" commands. \$\endgroup\$ – Dorian Nov 12 '19 at 10:12
3
\$\begingroup\$

Keg, 11 7 6 4 3 bytes

(⑩,

Explanation

(   # Repeat length of stack times:
 ⑩  # Print without popping the top of the stack
  , # Print with popping

Try It Online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I'm impressed! Could not have golfed it that much! About TIO for Keg: how would I go around doing that... I know that it is to be done via the chat room, but I don't know if a direct request from someone who has never had any involvement in the room would be perceived as rude. \$\endgroup\$ – Lyxal Jul 31 '19 at 10:37
  • \$\begingroup\$ Also, how would implicit input for Keg sound? \$\endgroup\$ – Lyxal Jul 31 '19 at 10:38
  • 3
    \$\begingroup\$ @Jono2906 That definitely wouldn't be rude! Leaving a ping to Dennis in talk.tryitonline.net with a link to the interpreter should be enough to get Keg on tio :) \$\endgroup\$ – Mr. Xcoder Jul 31 '19 at 12:14
  • \$\begingroup\$ @A__ Try it online! \$\endgroup\$ – Lyxal Aug 9 '19 at 23:04
  • 2
    \$\begingroup\$ This doesn't output anything now? \$\endgroup\$ – EdgyNerd Nov 3 '19 at 15:48
3
\$\begingroup\$

naz, 34 bytes

2a2x1v1x1f0a0x1x2f1r3x1v1e2o2f0x2f

Works for any input file terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v           # Set variable 1 equal to 2
1x1f0a           # Function 1
                 # Add 0 to the register
1x2f1r3x1v1e2o2f # Function 2
                 # Read a byte of input and jump to function 1 if it equals variable 1
                 # Otherwise, output twice and jump back to the start of function 2
2f               # Call function 2
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Fortran (GFortran), 71 65 bytes

character(99)a
read*,a
print*,(a(i:i),a(i:i),i=1,len_trim(a))
end

Try it online!

Saved 6 bytes thanks to @roblogic pointing out that if the input is quoted it can be read as list-directed.

If we don't care about extra whitespace:

Fortran (GFortran), 56 bytes

character(99)a
read*,a
print*,(a(i:i),a(i:i),i=1,99)
end

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice, I tried to use "proper" syntax for 100 bytes. codegolf.stackexchange.com/a/189152/15940 \$\endgroup\$ – roblogic Jan 19 at 9:30
  • 1
    \$\begingroup\$ @roblogic I saw, that's what inspired me to beat it. To me, there's nothing wrong with implicit loops in very rare situations (Array assignment and printing), but in golf, anything goes. \$\endgroup\$ – DeathIncarnate Jan 19 at 10:10
  • \$\begingroup\$ the 56-byte solution will work correctly if you assume the input string is quoted. example \$\endgroup\$ – roblogic Jan 19 at 10:34
  • \$\begingroup\$ @roblogic Still prints out excess whitespace, but saves bytes on the main one. \$\endgroup\$ – DeathIncarnate Jan 19 at 10:56
3
\$\begingroup\$

Pip, 17 16 13 11 bytes

Fi,#aL2Oa@i

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ (belatedly) You can save two more bytes by getting rid of the curly braces. \$\endgroup\$ – DLosc Dec 21 '19 at 9:00
2
\$\begingroup\$

Standard ML (MLton), 35 bytes

String.translate(fn c=>str c^str c)

Try it online!

The function translate from the String library has the following type:

translate : (char -> string) -> string -> string

Thus, given a function that takes a char c, converts it to a singleton string twice with str c and concatenates both with ^, we get the required functionality.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Japt -m, 1 byte

²

Try it online

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Was about to hit "Post" on my identical solution when the page refreshed with yours! \$\endgroup\$ – Shaggy Jul 31 '19 at 12:21
  • \$\begingroup\$ @Shaggy I knew this would be a race against you :-P \$\endgroup\$ – Oliver Jul 31 '19 at 12:22
  • 1
    \$\begingroup\$ A race against everyone, nearly, on a challenge this trivial! Posted a 2 byte solution anyway. \$\endgroup\$ – Shaggy Jul 31 '19 at 12:23
2
\$\begingroup\$

Japt, 2 bytes

Oliver beat me to the straightforward, 1 byte solution.

íU

Try it

Interleaves the input with itself.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

jq (-Rrj), 12 characters

(./"")[]|.+.

Sample run:

bash-5.0$ jq -Rrj '(./"")[]|.+.' <<< 'D0uB!e'
DD00uuBB!!ee

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Red, 43 bytes

func[s][t:""foreach c s[t: rejoin[t c c]]t]

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

VDM-SL, 28 bytes

f(a)==conc[[x,x]|x in seq a]

Distributed enumeration of the sequence containing sequences of length 2 of repeated elements of a.

enter image description here

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I don't think the output of your first solution is valid. \$\endgroup\$ – Shaggy Jul 31 '19 at 15:09
  • \$\begingroup\$ @Shaggy maybe, when I posted it there were other answers with similar output. I'll remove the first one \$\endgroup\$ – Expired Data Jul 31 '19 at 15:15
  • \$\begingroup\$ If you spot any others, leave them a similar comment ;) \$\endgroup\$ – Shaggy Jul 31 '19 at 17:10
2
\$\begingroup\$

Forth (gforth), 40 bytes

: x 0 ?do dup c@ dup emit emit 1+ loop ;

Try it online!

A byte can be saved by removing the ?, but that will break the handling of zero-length input.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

CJam, 3 bytes

q:_

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

><>, 9 bytes

i::0(?;oo

Try it online! I'm reasonably confident that this is optimal (barring solutions which terminate through errors; i:oo would be such a solution.)

Explanation

i::0(?;oo
i             takes one character of input      [c]
 ::           duplicates it twices              [c, c, c]
   0(?        if it's less than 0 (no input)    [c, c]
      ;       terminate
       oo     output twice                      []
              implicitly wrap to the beginning
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ If you can start with input on the stack (IIRC a command line option for one implementation), then you could do :oo for a solution with an error and l?!;:oo for a solution without. These are not tested, but I’m pretty sure they’re right. \$\endgroup\$ – cole Jul 31 '19 at 19:11
2
\$\begingroup\$

J, 2 bytes

2#

The verb "#", or "copy", copies its right argument the number of times specified by the left argument where the left argument is greater than or equal to zero and is either a single number or a vector of numbers of the same length as the first axis of the right argument. So, we can copy things "zero" times to remove them:

   1 0 1 0 # 1 2 3 4

returns 1 3.

Try it online: https://tio.run/##y/r//39qcka@gpGyukt@aVJOqkJxQWpitqI6138A

| improve this answer | |
\$\endgroup\$
  • 5
    \$\begingroup\$ Snippets aren't allowed. You solution needs to be assignable to a variable. So eg 2 # ], Galen's answer, is allowable because you can write f=. 2#], but you cannot write f=.2# \$\endgroup\$ – Jonah Aug 1 '19 at 2:08
2
\$\begingroup\$

Minkolang v.0.15, 7 bytes

od?.dOO

Try it here!

Explanation

od?.dOO
o            take one character of input
 d           duplicate top of stack
  ?.         terminate if its 0
    dOO      duplicate and output twice
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

8086/8088 machine code, 9 bytes

I couldn't resist.

ac       lodsb        Read DS:[SI] into AL, then increment SI.
aa       stosb        Write AL into ES:[DI], then increment DI.
3c 00    cmp al, 0    Is AL = 0?
74 03    je +5        If so, jump to the end of this loop.
aa       stosb        Write AL into ES:[DI], then increment DI.
eb f7    jmp -7       Jump to the beginning of this loop.

Assumptions:

  • The input string is a null-terminated string of bytes located at DS:SI.
  • There is an output string buffer located at ES:DI.
  • The output string buffer is large enough to hold the resulting string.
  • It's not necessary to tell the calling code the location of the output string buffer. If it is, add 2 bytes: 57 (push di) at the beginning and 5f (pop di) at the end. This assumes that the stack has 2 bytes free.
  • This code will be placed inline, so it's unnecessary to use an instruction to return. If this is false, add 1 byte: c3 (ret) at the end (after the 5f, if applicable).
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Or 7 bytes with just: lodsb / stosb / stosb / cmp al, 0 / jne -7 It will give you two 0's at the end of the string, but can't see how that's not allowed since you're writing to a "large enough" buffer and will still be null terminated. \$\endgroup\$ – 640KB Aug 1 '19 at 18:40
  • 2
    \$\begingroup\$ Or 6 bytes by combining the two ideas: lodsb / stosb / stosb / dec ax / jns -6 \$\endgroup\$ – 640KB Aug 1 '19 at 18:43
  • \$\begingroup\$ The question wording is pretty clear: It will modify this string. Not return a copy, so asking the caller to pass a 2nd output arg is definitely bending the rules. I asked for clarification on this. re: omitting the ret: it doesn't say "function" but it does say "return" instead of just "produce". To me that implies function, not inline block. \$\endgroup\$ – Peter Cordes Aug 3 '19 at 1:52
  • \$\begingroup\$ @gwaugh: dec ax only works if AH was known to be zero ahead of that. That would cost an extra 2-byte instruction ahead of the loop to zero AH or AX. You can't justify offloading that setup of a tmp reg to the "caller" in code-golf. dec al wouldn't be safe and is 2 bytes anyway so we might as well just use cmp. \$\endgroup\$ – Peter Cordes Aug 3 '19 at 2:01
  • \$\begingroup\$ Anyway, to loop backwards I was thinking std then a loop like lodsb/2xstosb/loop/cld (with the caller passing a length in CX). Although we probably need add si, cx and add di, cx first if the caller passes pointers to the start of the buffers. lea di, [esi + ecx*2] costs 4 bytes (address-size prefix + SIB) so it's the same as 2x add di, cx. I don't think there's a compact way to broadcast-load so we could do word stores. 32-bit mode for AVX2 vpbroadcastb xmm0, [esi] costs at least a 3-byte VEX prefix + opcode + ModRM. \$\endgroup\$ – Peter Cordes Aug 3 '19 at 2:07
2
\$\begingroup\$

Bash, 58 39 bytes

Using only shell builtins, no fancy parsers like sed.

58 bytes: Try it online

a="$*";for((i=0;i<${#a};i++)){ x=${a:i:1};echo -n "$x$x";}

47 bytes: (via primo)

for((;i<${#1};)){ x=${1:i++:1};echo -n "$x$x";}

39 bytes: (by manatwork). Try it online

while read -N1 c;do echo -n "$c$c";done
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Does not double % characters. \$\endgroup\$ – NieDzejkob Jul 31 '19 at 15:23
  • \$\begingroup\$ Good catch, fixed now (replaced printf with echo -n) \$\endgroup\$ – roblogic Jul 31 '19 at 15:42
  • 1
    \$\begingroup\$ No need to reassign $*, just use $1. Initializing i=0 is unnecessary. i++ can be moved to x=${1:i++:1}. \$\endgroup\$ – primo Jul 31 '19 at 16:49
  • 1
    \$\begingroup\$ A traditional read in while would be shorter and still using only Bash's own staff: Try it online!. \$\endgroup\$ – manatwork Jul 31 '19 at 17:33
2
\$\begingroup\$

Starry, 14 bytes

` , + + + . .'

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

33, 8 bytes

P[ktppP]

Try it online! Or not. How do you get a language on TIO?

The input string has to be null-terminated.

Explanation:

P     P  | Get input character
 [     ] | While it isn't a null character:
  kt     | - Put it in the string register
    pp   | - Print it twice
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Java (OpenJDK 8), 88 87 bytes

g->{String d ="";for(int i=0;i<g.length();){d+=""+g.charAt(i)+g.charAt(i++);}return d;}

Try it online!

Ungolfed:

g->{
    String d ="";
    for (int i=0;i<g.length();) {
        d+=""g.charAt(i)+g.charAt(i++);
    }
return d;
};

I'm sure there's way to shorten it, at least it was my first attempt in PPCG.

-1 for @manatwork

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Is shorter to force the casting (?) than to use 2 separate assignments; you can move the incrementation from for's 3rd argument to i's last usage: d+=""+g.charAt(i)+g.charAt(i++); \$\endgroup\$ – manatwork Aug 1 '19 at 11:31
  • \$\begingroup\$ @manatwork Oh, okay. Done \$\endgroup\$ – CuttingChipset Aug 1 '19 at 11:52
  • 4
    \$\begingroup\$ 48 \$\endgroup\$ – Grimmy Aug 1 '19 at 11:55
  • \$\begingroup\$ @CuttingChipset, actually the point of using a single assignment was to be able to remove the for block's braces. But with Grimy's suggestion, now that's meaningless. \$\endgroup\$ – manatwork Aug 1 '19 at 12:27
  • \$\begingroup\$ @manatwork but can I put his suggestion into the answer or what? It looks diametrically different than it is currently? \$\endgroup\$ – CuttingChipset Aug 1 '19 at 12:46
2
\$\begingroup\$

x86 machine code, 6 bytes

f:
  AC       LODSB
  AA       STOSB
  AA       STOSB
  E2 FB    LOOP  f
  C3       RET

Takes address of input string in SI, length of input string in CX, address of output string in DI. Should work in 16, 32 and 64-bit mode.

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ In 16-bit mode with far pointers the input string is in DS:SI and the output is in ES:DI of course. \$\endgroup\$ – Neil Aug 2 '19 at 9:51
  • \$\begingroup\$ The question specifies modifying a string (which implies in-place). So you need to work from the end of the string backwards to not overwrite chars you haven't read yet. I asked on the question for clarification. See also discussion on the 8086 machine-code answer. \$\endgroup\$ – Peter Cordes Aug 3 '19 at 2:46
2
\$\begingroup\$

Perl 6, 33 bytes

(@*ARGS[0].comb Xx 2).join.say

Comb splits our string into an array. The X meta operator applies the operator to it's right in a cross product fashion. So with the string "Double" it's doing ("D"x2, "o"x2, etc). Then we join the result and say it to print (with a newline).

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JavaScript (V8), 28 bytes

s=>[...s].map(n=>n+n).join``

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Bash, 72 bytes

s=$1;e(){ echo -n "${s:$i:1}";};for ((i=0;i<${#s};i++));do e;e;done;echo

If you don't have a bash terminal somewhere, this should work.

| improve this answer | |
\$\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.