[Question]
  [
### Challenge:
Given a string `s` on the characters `a`-`z`, `A`-`Z`, `0`-`9`, append the length of `s` to itself, counting the additional character(s) in the length as part of the total length of `s`.
### Input:
Just a string of arbitrary length (can be empty).
### Output:
The same string, but with its length appended to the end. The characters that represent the length should also be counted as part of the length. In cases where there are multiple valid lengths to append, choose the smallest one possible (see test cases for examples).
### Test Cases:
```
INPUT     -> OUTPUT       // Comment
aaa       -> aaa4
          -> 1            // Empty string
aaaaaaaa  -> aaaaaaaa9    // aaaaaaaa10 would also normally be valid, but violates using the smallest number rule mentioned above
aaaaaaaaa -> aaaaaaaaa11
a1        -> a13          // Input can contain numbers at the end of the string, you do not have to handle the fact that it looks like 13 rather than 3.
Longer test case(s):
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102
```
### Rules:
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code in bytes wins. Standard loopholes are forbidden. Submissions may be an entire program or a function, and you may either print the result to stdout or return it as a variable from a function.
  
[Answer]
## 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
```
[Answer]
# 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`
[Answer]
## 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>
```
[Answer]
## Lua 5.2, 32 Bytes
```
a=arg[1]print(a..#a+#(''..#a+1))
```
Where the variable *a* is the input string.
[Answer]
# C, ~~67~~ ~~65~~ 61 bytes
```
x;f(*v){printf("%d",(int)log10(x=-~printf(v))*-~(x%10>8)+x);}
```
[Wandbox](http://melpon.org/wandbox/permlink/vNFaprC3ZB9tthpp)
[Answer]
# Pyth - 7 bytes
```
+Qfql+Q
```
[Try it online here](http://pyth.herokuapp.com/?code=%2BQfql%2BQ&input=%22Aaaaaaaa%22&debug=0).
[Answer]
# 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.
[Answer]
## Pyke, 8 bytes ([old version](https://github.com/muddyfish/PYKE/commit/894a88e818e5e86643765030ce5565246630671f))
```
.f+liq)+
```
Explanation:
```
.f    )  -  first where (i++)
  +      -    input + i
   l     -    len(^)
    iq   -   ^ == i
       + - input + ^
```
[Try it here!](http://pyke.catbus.co.uk/?code=.fDQ%2Blq%29%2B&input=aaaaaaaaa) (New version, 9 bytes)
[Answer]
# 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**](https://tio.run/nexus/python2#@59mm5OYm5SSqFCsk2drYJWmAaS1DTUzgQzthLwEzeg8q9jUnOJUBTD3f7FtUWJ5fGZeQWmJhiZXeUZmDlDGiouzoCgzr0QhTSO1LDFHo1hTk4sTReV/JSUupcTERAgJBkjMRCUA)
[Answer]
## 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.
[Answer]
# [Brachylog](http://github.com/JCumin/Brachylog), 13 bytes
```
l<L$@:?rc.lL,
```
[Try it online!](http://brachylog.tryitonline.net/#code=bDxMJEA6P3JjLmxMLA&input=ImFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhIg&args=Wg)
### 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
```
[Answer]
# [Perl 6](https://perl6.org), ~~46~~ 35 bytes
```
{$_~(.chars,*.chars+.chars...{$^a==$^b})[*-1]}
```
```
{$_~(.chars,*.chars+.chars...*)[2]}
```
[Try it](https://tio.run/nexus/perl6#VVFBboMwELzziomKCiSOBUlVlSCSXnrovbeqihB1U1QCETZRURS@TteYCHraWc94dtaupcD5kaeRVRN6E1JFlnVscJ@WnwJxd7H3rcvT76SSbG7qwhTO@dx7X31cO5I/K7opEcO1gDXiLR4YIV@jgOEOL8eTaiBVlRUHIp40EWpJ2EsCDZ0kcHRHde30XC8L/J4NjdJfMcujjKc8KbAwc6n9KqtbiOUW9h4uNj@ice2sONVqmYvCY9ick7wWrl3W6nYIDxcypxWMklYYr6Bt8Voo7HaUycHvlJrNJl00WBhj7TGOmJgMI9p/tDYaW1oFyCT08w/hKfggYOCHTP/QtfsD "Perl 6 – TIO Nexus")
## 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
}
```
[Answer]
# 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!](http://brainfuck.tryitonline.net/#code=LD4rPC0tLS0tLS0tLS1bKysrKysrKysrKz4rWz4rPC1dLC0tLS0tLS0tLS1dPFs8XT5bLj5dPj4-KysrKysrKysrKzw8Wy0-Kz4tWz4rPj5dPlsrWy08Kz5dPis-Pl08PDw8PDxdPj5bLV0-Pj4rKysrKysrKysrPFstPi1bPis-Pl0-WytbLTwrPl0-Kz4-XTw8PDw8XT5bLV0-Pls-KysrKysrWy08KysrKysrKys-XTwuPDwrPis-Wy1dXTxbPFstPi08XSsrKysrK1stPisrKysrKysrPF0-LlstXV08PCsrKysrK1stPCsrKysrKysrPl08LlstXTw8Wy08Kz5d&input=YWtkamduYm12bmRhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYW9namRuZXJ1NWpnYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhMTI1NDc4OTY1MmFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWIK)
## 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](https://stackoverflow.com/a/13946554/1899794) 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.*
[Answer]
## 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.
[Answer]
## [Retina](https://github.com/m-ender/retina), 22 bytes
```
\G`
.
x
+r`\d*$
$._
x
```
[Try it online!](https://tio.run/nexus/retina#U9VwT/gf457ApcdVwaVdlBCToqXCpaIXD@T9/8@VmJgIwmAAZyCxkJk0ArS3gR5W0MWOUTAKRsEAgtFMPgpGwWguHwWjYBQMcQAA "Retina – TIO Nexus")
Ah well, if it wasn't for digits appearing in the input, this would be merely 11 bytes:
```
+r`\d*$
$._
```
[Answer]
# 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.
[Answer]
# [05AB1E](http://github.com/Adriandmen/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!](http://05ab1e.tryitonline.net/#code=W8K5wr5KRGfCvlEjwrxc&input=YWFhYWFhYWFh)
[Answer]
# PHP, 42 bytes
```
while(++$n<strlen($a=$argv[1].$n));echo$a;
```
Run with `-r`. Test [at OnlinePHPfunctions](http://sandbox.onlinephpfunctions.com/code/0b48cf43d8f9bd339f5c94f23dbf26a57541d861).
[Answer]
## 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.
[Answer]
# ><> (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.
[Answer]
# [Haskell](https://www.haskell.org/), ~~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!](https://tio.run/nexus/haskell#y03MzLPNzCtJLUpMLlFI/p9qm5Oal16SwZWjmldTbKNbnJFfrpKjbaiTZ5eqUGxbXJNro5unbWirARTTVM3lSgYJamsD5VSN/v9PpD0AAA "Haskell – TIO Nexus")
Recursive solution. Usage: 
```
Prelude> c "aaaaaaaaa"
"aaaaaaaaa11"
```
[Answer]
# Ruby, ~~43~~ 36 bytes
Anonymous function, [abusing](https://codegolf.stackexchange.com/a/92066/4372) 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"
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 6 bytes
```
LJL+LJ
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJQ4bmgIiwiIiwiTEpMK0xKIiwiIiwiYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYSJd)
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...).
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 8 bytes
```
L3Ya.#yy
```
[Try It Online!](https://dso.surge.sh/#@WyJwaXAiLCIiLCJMM1lhLiN5eSIsIiIsIiIsIlwiYWFhYWFhYWFhXCIiXQ==)
### Explanation
Inspired by [Pacmanboss256's Vyxal solution](https://codegolf.stackexchange.com/a/252736/16766).
```
          ;; 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"
```
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), 31 bytes
```
{$[9>t:#x;x,$1+t;x,$#x,($1+t)]}
```
[Try it online!](https://ngn.codeberg.page/k#eJxLs6pWiba0K7FSrrCu0FEx1C4BUcoVOhogtmZsLRdXWrRSYmKiUiyIASEToQCVh86F8aGUoVIsAKDMH2Y=)
*+22 bytes thanks to Dominic van Essen for pointing out a not-working test case (that should have worked)*
[Answer]
# C#, 77 bytes
```
n=>{int a=n.Length;int c=(a+1).ToString().Length-1;return(n+(n.Length+1+c));}
```
[Answer]
# [MATL](http://github.com/lmendo/MATL), 11 bytes
```
`G@Vhtn@>]&
```
[Try it online!](http://matl.tryitonline.net/#code=YEdAVmh0bkA-XSY&input=J2FhYWFhYWFhYSc) Or [verify all test cases](http://matl.tryitonline.net/#code=YFhLCmBLQFZodG5APl0mClhETiR4VA&input=J2FhYScKJycKJ2FhYWFhYWFhJwonYWFhYWFhYWFhJwonYTEnCidhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYScKJ2FhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYSc).
```
`      % 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
```
[Answer]
# 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
```
[Answer]
# 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 ]
```
[Answer]
**Clojure, 72 bytes**
```
(defn f([s](f s 1))([s n](if(=(count(str s n))n)(str s n)(f s(inc n)))))
```
]