Make a program that takes the word you input, and adds that word to the back of itself minus its first letter, then repeats until all letters are gone. For example, cat would become catatt, and hello would become helloellolloloo.

Any of the 26 letters of the English alphabet. There may be multiple words separated by spaces, and the change should be applied to every word.

The word(s) inputted, with each word put after itself with its first letter missing, and then with its second letter missing, and so on until there are no more letters to add.

More examples:

ill eel outputs illlll eelell

laser bat outputs laserasersererr batatt

darth vader outputs dartharthrththh vaderaderdererr

This is code golf, so the shortest code wins.

You can treat the input or output as a list. You can separate words using newline instead of space. You can add a trailing space to the input.

  • 22
    honestly, the multiple words thing is kinda annoying. All it does is require a split, apply the function on each word, and then join again. It's also quite debilitating for lots of esolangs which have to check for a space manually – Jo King Sep 18 at 22:44
  • 4
    Can we take in input as a list of words and output as such? – Quintec Sep 18 at 23:53
  • 4
    What length words do you need to handle? – MickyT Sep 19 at 1:14
  • 5
    Is it OK for words to be separated by a newline in the output(instead of a space)? – JayCe Sep 19 at 1:37
  • 10
    1. Please update the spec with the new allowances (array I/O, trailing space, etc.) 2. Please inform the existing solutions in case any can save bytes by taking advantage of them. – Shaggy Sep 19 at 8:36

65 Answers 65

up vote 34 down vote accepted

Japt -m, 6 3 bytes

Input and output are arrays of words.


Try it


        :For each word in the input array
£       :Map each letter at index Y
 sY     :  Slice the current word from index Y
  • 1
    Thats really compact. Nice! – qazwsx Sep 18 at 23:37
  • 9
    @qazwsx: Now 50% more compact! – Shaggy Sep 19 at 10:38
  • Isn't £ two bytes in UTF-8? – Vi. Sep 20 at 16:12
  • 7
    @Vi, I'm not using UTF-8 here. – Shaggy Sep 20 at 16:16

brainfuck, 60 56 bytes


Try it online!

Requires a trailing space and prints a leading space. Both of these could be circumvented, but that ends up at 112 bytes.


,[  Loop over each byte of input
  Tape: 32 w o r-32 d'
  >++++++++[-<----<++++>>]   Subtract 32 from the character and add 32 to the previous char
  Tape: 32 w o r d-32 0'
  <[>>]<   If the last character was a space
  Tape: 32 w o r d-32 0'
  Tape: 32 w o r d' space-32
    [<]>.   Move to the end of the word and print out the space
    [   Loop over each letter
      [-]    Remove the first letter (initially space)
      >[.>]  Print the rest of the word
      <[<]>  Move back to the first letter
    Tape: clear
,]  Get the next byte of input

Haskell, 36 21 bytes


Try it online!

Edit: -15 bytes, because of new IO format (list of words instead of space separated words)

  • You could shave off 5 characters by replacing scanr (:) "" with tails. – Frerich Raabe Sep 20 at 11:56
  • 1
    @FrerichRaabe: yes, but that would require an import Data.List which adds 17 bytes to the score. – nimi Sep 20 at 16:37

Perl -p, 36 25 23 bytes

s!\b|\S!$'=~s/ .*//r!eg

Try it online!

This is a single regsub. First, it matches all word boundaries or non-space characters:

[][H][e][l][l][o] [][W][o][r][l][d]

Note that each of these matches should be replaced with the rest of the word:

[→Hello][H→ello][e→llo][l→lo][l→o][o→] (...)

We can accomplish this with the special variable $', which stores the part of the string after the match. However, we need to apply the nested regsub s/ .*// to it, which removes everything past the first space in $', in order to get rid of the remaining words in the input.

Thanks to @nwellnhof for 2 bytes.

  • You can replace [^ ] with \S. – nwellnhof Sep 19 at 10:21

Python 3, 49 bytes

d=lambda s:' '.join(n+d(n[1:])for n in s.split())

Try It Online!

This takes advantage of the fact that "".split() returns an empty array so that acts as the check for the base case in the recursion.

Jelly, 3 bytes


Try it online!

Don’t need the Ks anymore since array input/output is now allowed.

  €   For each word:
Ḋ       Remove the first letter
 Ƭ      until there are none left.
  • I think you need ḊƬẎ) (or ḊƬF), if you prefer). – Erik the Outgolfer Sep 19 at 15:10
  • @EriktheOutgolfer I don't think so. Each word is represented by a separate array in the output – dylnan Sep 19 at 16:46
  • 1
    I'm not sure if you can claim that, since the arrays are nested, and nothing is specified in the question to allow it. – Erik the Outgolfer Sep 19 at 17:44

APL(Dyalog), 19 9 bytes


thanks to @H.PWiz for jogging my brain

This works because all strings in APL are character arrays.

        ¨ - for each string
      ⍵} - string argument - ex. "hello"
     ⌽ - reverse - "olleh"
   ,\ - scan magic - "o" "ol" "oll" "olle" "olleh"
  ∊ - enlist(join together) "oolollolleolleh"
{⌽ - reverse - "helloellolloloo"


JavaScript (ES6), 33 bytes

Saved 1 byte thanks to @ShieruAsakoto

I/O format: array of words.


Try it online!

JavaScript (ES6), 35 bytes

I/O format: array of words.


Try it online!

  • 2
    44: s=>s.replace(/\S+/g,g=s=>s&&s+g(s.slice(1))) – Shieru Asakoto Sep 19 at 0:43
  • 1
    Thanks for my "something new" for today; never knew about $' (or $<backtick>). – Shaggy Sep 19 at 22:07

R, 82 75 67 bytes


Try it online!

Several bytes saved thanks to JayCe

Separates output with newlines.

The sapply(...) expression generates a matrix/column vector of the appropriate substrings, padding with "" as needed. write then prints the elements of the matrix, y per line, separating them with "".

  • 4
    Golfed a different approach while holding a sleepy baby; will add an explanation later. – Giuseppe Sep 19 at 0:30
  • 2
    If the length of the words are restricted, eg 99 chars or ~1e6 then you can knock of a bunch of bytes with ...substring,1:1e6,1e6)... or similar – MickyT Sep 19 at 1:30
  • 2
    If you can separate words by a newline: tio. I have asked this in a comment. Can work with@MickyT ‘s comment – JayCe Sep 19 at 1:39
  • @JayCe looks like that could be 67 bytes before incorporating MickyT's suggestion – Giuseppe Sep 19 at 13:18

brainfuck, 94 93 bytes


Try it online!

  • Saved one byte thanks to Nitrodon -- golfing .[-]>[.>]<[<]> to [.>]<[<]>[-]>.


[[[ (dynamic) tape layout: ... NUL STR ... STR CHR FLG BUF SPC NUL ... ]]]

load a 32 into SPC

while FLG

 read a word
  clear FLG; read CHR
  copy CHR to BUF (using FLG as a temporary)
  subtract SPC from BUF and save SPC
  move tape layout one to the right

 strip trailing space; set FLG to true
 << [>>+<<[-]]
 to STR's first character
 print word in all reduced forms

 print SPC; move to FLG

Try it online!


Esolang's brainfuck constant collection was used for the initial space load.

  • This doesn't seem to terminate. Is that intended? – Jo King Sep 18 at 23:34
  • 1
    @JoKing Yes. In certain implementations it would exceed the tape limit, exiting by error. – Jonathan Frech Sep 18 at 23:35

05AB1E, 5 bytes


Try it online!


€.s        # push suffixes of each
   í       # reverse each
    J      # join suffixes
  • 1
    Boring 5-bytes alternative: í€ηJí (since prefixes is a 1-byte builtin instead of 2-bytes like suffixes; still requires an additional reverse-each however at the start however, so the byte-count remains 5). – Kevin Cruijssen Sep 19 at 13:19

Vim, 47 bytes (38 key strokes)

Start with your input as the sole line in a Vim buffer.



This puts each word on its own line, iterates over each line, then rejoins them all. Breaks if words are longer than 99 characters or if your input has more than 99 words.

  1. :s/<Space>/\r/g<CR> replaces spaces with new lines (\r)
  2. gg positions the cursor at the beginning of the first line
  3. qa begins recording macro a:
    • yw yanks the rest of the word
    • P puts it behind the cursor
    • lx removes the first letter of the latter word
    • q stops recording macro a
  4. qb begins recording macro b:
    • 99@a executes macro a ninety-nine times (introduces the character limit)
    • j0 positions the cursor at the start of the next line
    • q stops recording macro b
  5. 99@b executes macro b ninety-nine times (introduces the word limit)
  6. gg positions the cursor at the first line
  7. 99J joins the following ninety-nine lines with spaces (word limit again)

For another 2 bytes (2 key strokes) you could extend the word limit to 999. Another 4 bytes, 9999, etc.

Husk, 6 4 bytes

-2 bytes thanks to Jonathan Allan (taking input as a list)!


Try it online!


Takes input as a list of strings and maps the following function:

Σṫ  -- example argument: "abc"
 ṫ  -- tails: ["abc","bc","c"]
Σ   -- concat: "abcbcc"
  • The split & join are possibly no longer required, currently such specification is in a comment. – Jonathan Allan Sep 19 at 17:23

Retina 0.8.2, 15 bytes


Try it online! Note: trailing spaces. Explanation:

Split on spaces.


Append its suffix to each letter. The % means that we only get the word's suffix.

Join with spaces.

Pepe, 167 153 bytes


Try it online!

16-bit x86 assembly code, 24 bytes

     47             inc    di
     B020           mov    al,20h
l1:  3806           cmp    [si],al
     7212           jb     l5 ;less means end of string
     7401           je     l2  ;equal means space was seen
     4F             dec    di ;overwrite extra space
l2:  E80300         call   l3
     46             inc    si ;move to next character in word
     75F1           jne    l1
l3:  56             push   si
l4:  3806           cmp    [si],al
     A4             movsb      ;copy character
     77FB           ja     l4  ;until either zero or space is seen
     5E             pop    si
l5:  C3             ret

Call with si = pointer to source string, di = pointer to output buffer.
The source string requires a zero byte to end it.
The code is the same in 16- or 32- or 64-bit (si/di become either esi/edi or rsi/rdi).
32-bit code is two bytes larger because of the expanded call.
64-bit code is three bytes larger still because the inc/dec of rsi/rdi attracts a prefix (but if it is known that they are within 32-bit memory space, then they can be esi/edi again to avoid that penalty).

MATL, 18 16 bytes


Input is a cell array of words. Try it online!


"         % Implicit input: cell array of strings. For each cell
  @g      %   Push content of current cell, that is, a word
  XH      %   Copy into clipboard H
  "       %   For each letter
    H     %     Push word
    X@    %     Push iteration index
    Jh)   %     Index from that until the end into current word
  ]       %   End
  0       %   Push 0. Will be cast to char. Char(0) is displayed as space
  &h      %   Concatenate horizontally all elements so far. Implicit display

K4 / K (oK), 9 bytes



Try it online!


,/'(1_)\' / the solution
        ' / apply to each
       \  / scan
   (  )   / do this together
    1_    / drop first
,/'       / flatten (,/) each (')

C++ (clang), 174 bytes

std::string r(std::string w){while(auto x=strchr(w.c_str(),32))return r(w.substr(0,x-w.c_str()))+" "+r(x+1);return w!=""?w+r(w.substr(1)):w;}

Try it online!

It's my first submission, and i didn't know if returning string instead of printing it is okay :)

  • 2
    Welcome to PPCG! Yes, returning a string is okay. Hope you stick around! – Jo King Sep 20 at 10:27
  • You could use the inequality operator's symmetry to remove a space and thus save a byte -- return w!=""? can be return""!=w?. – Jonathan Frech Sep 21 at 19:27

Stax, 3 bytes


Run and debug it


m   Map over the lines
 |] Get all suffixes (suffices?)
    Implicit flatten and output

Charcoal, 14 bytes

⪫E⪪S ⭆ι✂ιμLι¹ 

Try it online! Note: Trailing space. Link is to verbose version of code. Explanation:

   S            Input string
  ⪪             Split on spaces
 E              Map over each word
      ι         Current word
     ⭆          Map over each character and join
        ι       Current word
         μ      Current index
           ι    Current word
          L     Length
            ¹   Literal 1
       ✂        Slice
⪫               Join with spaces
                Implicitly print

C (gcc), 79 bytes


Try it online!

Pip -s, 11 bytes


Takes the space-separated list of words from stdin. Try it online!


             s is space (implicit)
        q    Read a line of stdin
         ^s  Split it on spaces
       M     Map this lambda function to each word:
 _            The word...
  @>          sliced starting at index...
    ,#_       range(len(word))
              This creates len(word) slices ["word" "ord" "rd" "d"]
J             Join those into a single string
             The resulting list of modified words is printed; the -s flag uses space
             as the separator

Ruby, 42 bytes


Try it online!

Python 2, 63 bytes

lambda s:' '.join(map(g,s.split()))
g=lambda s:s and s+g(s[1:])

Try it online!

  • It is 64 bytes on my computer, mac OS. – aydinugur Sep 21 at 23:30

Canvas, 6 bytes


Try it here!

5 bytes with a crazy output format

C#, 111 90 bytes

b=>string.Join(" ",(b.Split(' ').Select(x=>string.Concat(x.Select((y, i)=>x.Substring(i))))))

Try it Online!

By Changing input and output to arrays, I saved a few bytes:


Try it Online!

K (oK), 17 13 bytes


Try it online!

Prefix anonymous function; Input is taken as a list of strings, which in turn are lists of characters.

Thanks @streetster for 4 bytes.


{,/|:'|,\|x}' //Main function, argument x → ("ill";"eel")
            ' // For each element of the argument
         |x}  // Flip it. x → ("lli";"lee")
       ,\     // Concatenate each element, keeping intermediates. x → (("l";"ll";"lli");("l";"le";"lee")
      |       // Flip it again. x → (("lli";"ll";"l");("lee";"le";"l"))
   |:'        // Now flip each element. x → (("ill";"ll";"l");("eel";"el";"l"))
{,/           // Concatenation scan. x → ("illlll";"eelell")
  • You can return a list, also take a look at my oK solution – streetster Sep 20 at 16:33
  • @streetster oh, nice. I'm still in the process of learning K, so my solutions won't be as short or as elegant as I'd like. Thanks for the heads up! – J. Sallé Sep 20 at 16:35
  • Flatten before reverse allows you to omit the "reverse-each", bringing it down to 10 bytes: {|,/,\|x}' – hoosierEE Oct 19 at 2:39

Common Lisp, 179 bytes

(defun r(s)(cond((endp s)nil)((eql(first s)#\Space)(princ " ")(r(rest s)))(t(q s)(r(rest s)))))(defun q (l)(cond((eql(first l)#\Space)t)((endp l)t)(t(princ(first l))(q(rest l)))))

Try it online!

This is my first try at golfing any edits are welcome

  • Hello and welcome to PPCG. Removing whitespace can save you 29 bytes. – Jonathan Frech Sep 21 at 1:20
  • @Johnathan Frech thanks i just updated with no spaces – JRowan Sep 21 at 1:27
  • I think you missed four superfluous spaces. – Jonathan Frech Sep 21 at 1:28
  • You can most likely also use car instead of first and cdr instead of rest to further golf your submission. – Jonathan Frech Sep 21 at 1:30
  • Na, im good with it now haha, maybe ill come back and mess with it later. Im just learning lisp now my teacher said to never use car and cdr so they were out of my head while i was doing it – JRowan Sep 21 at 1:32

Lua, 70 bytes

for i=1,#arg do x=arg[i]for i=1,#x do io.write(x:sub(i))end print()end

Try it online!


The arguments in Lua are stored in the table arg starting at index 1. The unary operator # returns the size of the table and function s:sub(a,b) returns a substring based on string s delimited by integers a and b, if b is not passed it will return the rest of the string.

I had to use io.write() instead of print() to avoid line breaking, and added print() at the end for the opposite reason.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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