g | x | w | all
Bytes Lang Time Link
011J240728T020039Znoodle p
325Vyxal240728T002201Zlyxal
125Pascal240727T160000ZKai Burg
004Thunno 2230714T094026ZThe Thon
029JavaScript221208T150035ZShaggy
031K ngn/k221007T085640Zoeuf
00405AB1E221205T071349ZKevin Cr
014Ly221007T101412Zcnamejj
047Factor221007T012411ZRazetime
008Pip221006T232520ZDLosc
006Vyxal221006T175007Zpacman25
036Ruby170115T155117Zdaniero
067Java 7170126T145824ZPoke
031Perl170115T131255Zuser6440
061C161216T221512Zuser5634
060Haskell161217T000335ZLaikoni
012Jelly161225T153834ZSherlock
079C# / CS Script161223T113508ZErresen
027awk161223T091854ZJames Br
037awk161223T215207ZEd Morto
083ForceLang161223T140517ZSuperJed
035><> Fish161223T100041ZTeal pel
075Scala161222T091131ZMinimal
nan161219T101525ZKieron D
041Labyrinth161218T203735ZRobert H
047bash161218T200032ZMitchell
056Wolfram161218T183940Zswish
042PHP161218T180246ZTitus
049R161217T084515ZFré
032JavaScript ES6161216T225102ZArnauld
072Clojure161218T134423ZNikoNyrh
039Python161218T093026Zxnor
258Brainfuck161217T160114ZForcent
01105AB1E161217T224759ZOsable
nan161217T223237ZBrad Gil
056Ruby161217T000625ZElenian
055Factor161217T123547Zcat
075C161217T125416Zcat
013Brachylog161217T085812ZFatalize
057Mathematica161217T065732ZGreg Mar
nanLaTeX161217T043127ZC5H8NNaO
051Ruby161217T011915ZDepresse
046Haskell161217T005400Znimi
007Pyth161217T001052ZMaltysen
011MATL161216T213533ZLuis Men
022Retina161216T230142ZMartin E
046Python 2161216T223649Zmbomb007
077C#161216T222405ZAlfie Go
008Pyke161216T214455ZBlue
032Lua 5.2161216T215114Zresin
037JavaScript ES6161216T214025ZNeil

J, 11 bytes

(,":@#)^:3~

Attempt This Online!

(,":@#) is a dyadic hook which takes strings on the left and right and gives the left string joined with the right string's length.

^:3 means repeat 3 times.

~ means use the same value for both the right and left arguments, so in the first iteration the string gets joined with its own length, then in the second iteration the original string gets joined with the length of that, etc.

Vyxal, 26 bitsv2, 3.25 bytes

3(LJ

Try it Online!

Bitstring:

01001000011111010110110111

Finally posting my golfing suggestion as a separate answer. 3 times, len top and append.

Pascal, 125 B

String capabilities and writeStr are defined by Extended Pascal, ISO standard 10206. The built‑in constant maxInt has been specified only for scoring reasons: you probably need to replace maxInt by a reasonably small value not exhausting available memory.

type t=string(maxInt);function f(s:t)=r:t;var n:integer;begin
r:=s;repeat n:=length(r);writeStr(r,s,n:1)until n=length(r)end;

Readablified:

type
    stringMaximum = string(maxInt);

function appendStringLength(protected s: stringMaximum) = result: stringMaximum;
    var
        printedLength: 0‥s.capacity;
    begin
        result ≔ s;
        
        repeat
        begin
            printedLength ≔ length(result);
            writeStr(result, s, printedLength:1)
        end
        until length(result) = printedLength
    end;

Thunno 2, 4 bytes

3{l+

Try it online!

Explanation

3{l+  # Implicit input
3{    # Repeat three times:
  l   #  Take the length of the top
      #  of stack (initially input)
   +  #  And append this to the input
      # (Three times is enough to handle
      #  digit overflows like 9, 99, etc.)
      # Implicit output

JavaScript, 29 bytes

s=>(g=x=>s+x.length)(g(g(s)))

Try it online!

K (ngn/k), 31 bytes

{$[9>t:#x;x,$1+t;x,$#x,($1+t)]}

Try it online!

+22 bytes thanks to Dominic van Essen for pointing out a not-working test case (that should have worked)

05AB1E, 4 bytes

3Fg«

Try it online or verify all test cases.

Explanation:

3F    # Loop 3 times:
  g   #  Pop the current string, and push its length
      #  (which will use the implicit input-string in the first iteration)
   «  #  Append this length to the (implicit) input-string
      # (after which the result is output implicitly)

Ly, 26 14 bytes

iysp&ol`Syl+u;

Try it online!

This turned out to be trickier than I expected... Since strings of length 9, 99, etc... need an extra digit when the length it appended.

So the code computes the number of digits in the length of a string composed of the original string plus the length of that string. Then that length is added to the length of the original string to get the final length, which is appended to the original.

iysp&ol`Syl+u; - 
i              - read in the input string
 ysp           - get the length, stash it, delete from stack
    &o         - print the string
      l`       - load the string length and increment
        Sy     - split to digit, count them
          l+   - load the string length and add to digit count
            u; - print as number and exit (to avoid printing stack)

jq, 40 bytes

.+"\(length+(length+1|tostring|length))"

Try it online!

It's the same algorithm, just in jq for comparison. I couldn't find a shorter way to do it in jq even though using "length" three times seems like it should be golf-able...

Factor, 47 bytes

[ dup length dup log10 ⌈ + 1 /i >dec append ]

Attempt This Online!

small improvement to the previous answer.

>dec is polyfilled since it is a newer addition.

Pip, 8 bytes

L3Ya.#yy

Try It Online!

Explanation

Inspired by Pacmanboss256's Vyxal solution.

          ;; y is empty string; a is first command-line argument (implicit)
L3        ;; Loop 3 times:
   a.     ;;  Concatenate a with
     #y   ;;  Length of y
  Y       ;;  Assign the result to y
       y  ;; After the loop, output y

Some worked examples:

Input a        ""    "aaa"    "aaaaaaaaa"
Original y     ""    ""       ""
After loop 1   "0"   "aaa0"   "aaaaaaaaa0"
After loop 2   "1"   "aaa4"   "aaaaaaaaa10"
After loop 3   "1"   "aaa4"   "aaaaaaaaa11"

Vyxal, 6 bytes

LJL+LJ

Try it Online!

Append the length of the string, take the length of that, then append the new length to the original. This does work with digit overflows (9, 99, etc...).

Ruby, 43 36 bytes

Anonymous function, abusing the $. global.

->s{$.+=1while(t=s+"#$.").size>$.;t}

Call it like this:

f = ->s{$.+=1while(t=s+"#$.").size>$.;t}
f[""]          # => "1"
f["a"]         # => "a2"
f["aaaaaaa"]   # => "aaaaaaa8"
f["aaaaaaaa"]  # => "aaaaaaaa9"
f["aaaaaaaaa"] # => "aaaaaaaaa11"

Java 7, 67 bytes

String y(String y){int i=0;for(;(y+i).length()!=i;i++);return y+i;}

Try it online!

Perl, 31 bytes

Just going through all the numbers until one fits:

perl -lpe '1while++$n<length$_.$n;$_.=$n'

C, 67 65 61 bytes

x;f(*v){printf("%d",(int)log10(x=-~printf(v))*-~(x%10>8)+x);}

Wandbox

Haskell, 61 60 bytes

e=length
l%n|s<-show$l+1,n>e s=s|m<-n+1=(l+1)%m
c s=s++e s%2

Try it online!

Recursive solution. Usage:

Prelude> c "aaaaaaaaa"
"aaaaaaaaa11"

Jelly, 12 bytes

Many thanks to Dennis for his help in writing this answer. Golfing suggestions welcome. Try it online!

D;³L=
LÇ1#;@

Explanation

LÇ1#;@  Main link. Argument: s
          Runs the helper link until a suitable x is found.
L       Yields the length of s.
 Ç1#    Calls the helper link until one match is found.
    ;@  Appends the matching x to s.

D;³L=  Helper link. Argument: x
         Appends x to s and checks if its length is equal to x.
D      Convert x from integer to decimal.
 ;³    Append to our original string.
   L=  Check if the length of this new string is equal to x.

C# / CS Script 99 87 79 bytes

Saved a few bytes switching to a lambda. And a few more moving the try/catch out of the for loop.

F=a=>{int i=0,l;try{for(;;)l=(a+(a+a.Length).Length)[++i];}catch{}return a+i;};

Try it here

Expanded:

F => a
{
    // 'i' is for traversing the string
    // 'l' is just a placeholder
    int i = 0, l;

    // wrap the loop in a try/catch
    try
    {
        // loop forever
        for (;;)
            // assign to 'l' the char at position ++i
            // throws an IndexOutOfRangeException when it falls off the end
            l = (a + (a + a.Length).Length)[++i];
    }
    // empty catch block
    catch {}

    // returns a string concat of the input a and index i
    return a + i;
}

awk, 27 bytes

$0=$0length($0length($0NF))

Test it:

$ awk -F '' '$0=$0length($0length($0NF))' file
7aaaaaa8
8aaaaaaa9
9aaaaaaaa11
97aaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa99
98aaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa101

awk, 37 bytes

{FS="";r=$0;$0=r NF;$0=r NF;$0=r NF}1

That will work in any awk that splits lines into characters when FS is NULL e.g. GNU awk and most others. WIth other awks you'd have to use the length() function instead of NF to calculate the number of characters in the record, e.g. awk '{r=$0;$0=r length;$0=r length;$0=r length}1' file.

Breakdown:

FS=""    # split the record at every character so NF is a count of characters in the record
r=$0     # save the original record
$0=r NF  # append the original length to the original record and cause awk to recalculate NF
$0=r NF  # append the new length to the original record and cause awk to recalculate NF again
$0=r NF  # append the final length to the original record for output
1        # a true condition invoking awks default action of printing the current record

Example:

$ awk '{FS="";r=$0;$0=r NF;$0=r NF;$0=r NF}1' file
aaa4
1
aaaaaaaa9
aaaaaaaaa11
a13
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102

ForceLang, 83 bytes

set s io.readln()
label 1
set n 1+n
set t s+n
if t.len=n
 io.write t
 exit()
goto 1

><> (Fish) 35 bytes

i:1+?!v:o
ln;v9l<  >
*9+>:&)?!^1l&a

Takes input onto the stack, checks the length against values 9,99,999... and if the length is larger than add 1 to the stack length.

Scala, 75 bytes

def a(b:String,c:Int=0):String=if((b+c).length>c)a(b,c+1)else b+c

Under review

Powershell V2.0, 64 bytes

$s=$args[0]
$s+[string]($s.length+([string]($s.length)).length)

Example

PS C:\Users\***\Downloads\golf> .\str-length.ps1 'This is my strin3333333333333333333333333333333333333333333333333333
33333333333333333333333333333333333333333g'
This is my strin333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333g113

Labyrinth, 48 45 41 bytes

)"   10/{:@!
.,;: _ { _ ;
   })"}) 10-9!@

Try it online!

Saved 4 bytes thanked to @Martin Ender

bash, 47 bytes

 for((n=-1;${#s} != $n;));{ s=$1$[++n];};echo $s

Save this as a script, and pass the input string as an argument.

It's a brute force implementation: try each number in turn until you find one which works.

Wolfram, 56

#<>ToString@Nest[l+IntegerLength@#&,l=StringLength@#,2]&

Given l = StringLength[x] it appends l + IntegerLength[l + IntegerLength[l]] to x.

PHP, 42 bytes

while(++$n<strlen($a=$argv[1].$n));echo$a;

Run with -r. Test at OnlinePHPfunctions.

R, 49 bytes

cat(a<-scan(,""),(t<-nchar(a))+nchar(t+1),sep='')

Pretty straightforward solution.

JavaScript (ES6), 32 bytes

f=(s,n=0)=>(s+n)[n]?f(s,n+1):s+n

How it works

f = (s, n = 0) =>   // given a string 's' and starting with n = 0:
  (s + n)[n] ?      // if the Nth character of (s + n) exists:
    f(s, n + 1)     //   try again with n + 1
  :                 // else
    s + n           //   return s + n

Starting with N=0, we test the Nth character (0-based) of the string made of the concatenation of the original input string and the decimal representation of N. We increment N until this character doesn't exist anymore.

Example:

N =  0 : abcdefghi0
         ^
N =  1 : abcdefghi1
          ^
N =  2 : abcdefghi2
           ^
...
N =  8 : abcdefghi8
                 ^
N =  9 : abcdefghi9
                  ^
N = 10 : abcdefghi10
                   ^
N = 11 : abcdefghi11    -> success
                    ^

Test cases

f=(s,n=0)=>(s+n)[n]?f(s,n+1):s+n

console.log(f("aaa"));       // -> aaa4
console.log(f(""));          // -> 1
console.log(f("aaaaaaaa"));  // -> aaaaaaaa9
console.log(f("aaaaaaaaa")); // -> aaaaaaaaa11
console.log(f("a1"));        // -> a13

Clojure, 72 bytes

(defn f([s](f s 1))([s n](if(=(count(str s n))n)(str s n)(f s(inc n)))))

Python, 39 bytes

lambda a:eval('a+str(len('*3+'a))))))')

Longer form:

lambda a:a+str(len(a+str(len(a+str(len(a))))))

Iteratively in Python 2 (41 bytes):

x=a=input();exec"x=a+`len(x)`;"*3;print x

Starting with x as the input string a, applies the transformation x -> a + str(len(x)) three times. I'm still not clear why three applications are needed to always reach the fixed point.

Brainfuck, 258 bytes

,>+<----------[++++++++++>+[>+<-],----------]<[<]>[.>]>>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]

The input must be terminated by a linefeed (LF). Only works for inputs with a length lesser than 256 (including the LF).

Try it online!

Explanation

# read first char and add one to cell #1
# the cell after the input will contain the length
,>+<
# subtract 10 to check for LF
----------
# while the input is not 10 (LF)
[
# restore the input to its original value
++++++++++
# add one to the length
>+
# cut and paste the length to the next cell, then read the input
[>+<-],
# subtract 10 to check for LF
----------
]
# for input abc, the tape here would be: a b c *0* 4
# rewind to the beginning of the input
<[<]>
# print the input string
[.>]>
# convert the length to ascii chars and output them
>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-
<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++
<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]

Note: I used code from this SO answer to convert the length to ascii output; I hope this is acceptable on PPCG. This is my first Codegolf submission, and my second BF program. Feedback is welcomed.

05AB1E, 11 bytes

[¹¾JDg¾Q#¼\

Pretty straightforward bruteforce:

            Implicit i = 0
[           while true
 ¹¾J        Concatenate input and i -> str
    Dg¾Q#   Break if length(str) == i
         ¼\ Else, i += 1

Try it online!

Perl 6,  46  35 bytes

{$_~(.chars,*.chars+.chars...{$^a==$^b})[*-1]}
{$_~(.chars,*.chars+.chars...*)[2]}

Try it

Expanded:

{   # bare block lambda with implicit parameter 「$_」

  $_  # the input

  ~   # concatenated with

  (  # sequence generator

    .chars,  # the number of chars in 「$_」 (seed the generator)


    *\      # Whatever lambda input (represents previous value)
    .chars  # number of chars in that
    +       # plus
    .chars  # the number of chars in 「$_」


    ...     # keep doing that until

    *       # indefinitely

  )[2] # get the value at index 2 of the sequence
}

Ruby, 62 58 56 bytes

s=gets.chomp;p s+"#{(s+"#{(s+"#{s.size}").size}").size}"

Tested in irb.

There's probably a better way to do this, but this was the first thing I came up with. Any help in golfing would be appreciated.

edit: I realized my use of parentheses was excessive.

Factor, 55 bytes

It's a walk in the park! I came up with this in my head as soon as I read the question.

[ dup length dup log10 ⌈ + >integer 10 >base append ]

C, 75 bytes

Yes, there's another C answer but this (the same method as my Factor answer) is shorter:

i,z;f(char*s){i=strlen(s);z=1+log10(i);sprintf(realloc(s,i+z)+i,"%d",i+z);}

Ungolfed:

i, z;

g (char* s){
  i = strlen(s);
  z = 1 + log10(i);
  s = realloc(s, i + z);
  sprintf(s + i, "%d", i + z);
}

ceil(log10(n) is the number of digits in base 10 n, but 1+ is shorter than ceil().

Then, resize the string so we don't get a segfault, and format the number after the string part.

Brachylog, 13 bytes

l<L$@:?rc.lL,

Try it online!

Explanation

Basically a description of the problem. It will try every value of L bigger than the length of the input until it finds one for which, when concatenated to the input, is the length of that concatenation.

l<L              length(Input) < L
  L$@            Convert L to a string
     :?rc.       The Output is the concatenation of the Input with L as string
         .lL,    The length of the Output is L itself

Mathematica, 57 bytes

#<>ToString[(a=Length@#)+(i=IntegerLength)[a+i@a]~Max~1]&

Unnamed function taking an array of characters as input and returning a string. Uses the fact that if a is the length of the input, then the number to append to the input is a plus the number of digits in (a + the length of a), rather than just a plus the number of digits of a. Unfortunately it wouldn't give the right answer for the empty-string input without the ~Max~1 special case.

LaTeX, 108/171

\newcounter{c}\def\s#1#2]{\stepcounter{c}\def\t{#2}\ifx\empty\t\arabic{c}\else#1\s#2]\fi}\def\q[#1]{\s#10]}

\q[] //1

Ruby, 51 bytes (program)

Ruby, 49 bytes (function)

Program (last newline is not necessary and thus unscored):

x=gets.strip
i=0
i+=1 until(y=x+i.to_s).size==i
p y

Function (last newline is scored):

def f x
i=0
i+=1 until(y=x+i.to_s).size==i
y
end

Haskell, 46 bytes

f s=[l|i<-[0..],l<-[s++show i],length l==i]!!0

Usage example: f "aaaaaaaa" -> "aaaaaaaa9".

Simply try all numbers starting with 0 and take the first that fits.

Pyth - 7 bytes

+Qfql+Q

Try it online here.

MATL, 11 bytes

`G@Vhtn@>]&

Try it online! Or verify all test cases.

`      % Do...while
  G    %   Push input
  @    %   Push iteration index (1-based)
  V    %   Convert number to string
  h    %   Concatenate horizontally
  t    %   Duplicate
  n    %   Get length of concatenated string
  @    %   Push iteration index
  >    %   True if length of concatenated string exceeds iteration index
]      % End. Run next iteration if top of stack is true; else exit loop
&      % Specifiy that next function (implicit display) takes only one input
       % Implicitly display top of the stack. This is the concatenated string
       % that had a length equal to the iteration index

Retina, 22 bytes

\G`
.
x
+r`\d*$
$._
x

Try it online!

Ah well, if it wasn't for digits appearing in the input, this would be merely 11 bytes:

+r`\d*$
$._

Python 2, 54 48 46 bytes

Simple solution. Recursion ended up being shorter.

f=lambda s,n=0:f(s,n+1)if(s+`n`)[n:]else s+`n`

Try it online

C#, 77 bytes

n=>{int a=n.Length;int c=(a+1).ToString().Length-1;return(n+(n.Length+1+c));}

Pyke, 8 bytes (old version)

.f+liq)+

Explanation:

.f    )  -  first where (i++)
  +      -    input + i
   l     -    len(^)
    iq   -   ^ == i
       + - input + ^

Try it here! (New version, 9 bytes)

Lua 5.2, 32 Bytes

a=arg[1]print(a..#a+#(''..#a+1))

Where the variable a is the input string.

JavaScript (ES6), 37 bytes

f=(s,t=s,u=s+t.length)=>t==u?t:f(s,u)
<input oninput=o.textContent=f(this.value)><pre id=o>