| Bytes | Lang | Time | Link |
|---|---|---|---|
| 011 | J | 240728T020039Z | noodle p |
| 325 | Vyxal | 240728T002201Z | lyxal |
| 125 | Pascal | 240727T160000Z | Kai Burg |
| 004 | Thunno 2 | 230714T094026Z | The Thon |
| 029 | JavaScript | 221208T150035Z | Shaggy |
| 031 | K ngn/k | 221007T085640Z | oeuf |
| 004 | 05AB1E | 221205T071349Z | Kevin Cr |
| 014 | Ly | 221007T101412Z | cnamejj |
| 047 | Factor | 221007T012411Z | Razetime |
| 008 | Pip | 221006T232520Z | DLosc |
| 006 | Vyxal | 221006T175007Z | pacman25 |
| 036 | Ruby | 170115T155117Z | daniero |
| 067 | Java 7 | 170126T145824Z | Poke |
| 031 | Perl | 170115T131255Z | user6440 |
| 061 | C | 161216T221512Z | user5634 |
| 060 | Haskell | 161217T000335Z | Laikoni |
| 012 | Jelly | 161225T153834Z | Sherlock |
| 079 | C# / CS Script | 161223T113508Z | Erresen |
| 027 | awk | 161223T091854Z | James Br |
| 037 | awk | 161223T215207Z | Ed Morto |
| 083 | ForceLang | 161223T140517Z | SuperJed |
| 035 | ><> Fish | 161223T100041Z | Teal pel |
| 075 | Scala | 161222T091131Z | Minimal |
| nan | 161219T101525Z | Kieron D | |
| 041 | Labyrinth | 161218T203735Z | Robert H |
| 047 | bash | 161218T200032Z | Mitchell |
| 056 | Wolfram | 161218T183940Z | swish |
| 042 | PHP | 161218T180246Z | Titus |
| 049 | R | 161217T084515Z | Fré |
| 032 | JavaScript ES6 | 161216T225102Z | Arnauld |
| 072 | Clojure | 161218T134423Z | NikoNyrh |
| 039 | Python | 161218T093026Z | xnor |
| 258 | Brainfuck | 161217T160114Z | Forcent |
| 011 | 05AB1E | 161217T224759Z | Osable |
| nan | 161217T223237Z | Brad Gil | |
| 056 | Ruby | 161217T000625Z | Elenian |
| 055 | Factor | 161217T123547Z | cat |
| 075 | C | 161217T125416Z | cat |
| 013 | Brachylog | 161217T085812Z | Fatalize |
| 057 | Mathematica | 161217T065732Z | Greg Mar |
| nan | LaTeX | 161217T043127Z | C5H8NNaO |
| 051 | Ruby | 161217T011915Z | Depresse |
| 046 | Haskell | 161217T005400Z | nimi |
| 007 | Pyth | 161217T001052Z | Maltysen |
| 011 | MATL | 161216T213533Z | Luis Men |
| 022 | Retina | 161216T230142Z | Martin E |
| 046 | Python 2 | 161216T223649Z | mbomb007 |
| 077 | C# | 161216T222405Z | Alfie Go |
| 008 | Pyke | 161216T214455Z | Blue |
| 032 | Lua 5.2 | 161216T215114Z | resin |
| 037 | JavaScript ES6 | 161216T214025Z | Neil |
J, 11 bytes
(,":@#)^:3~
(,":@#) 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
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+
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
K (ngn/k), 31 bytes
{$[9>t:#x;x,$1+t;x,$#x,($1+t)]}
+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;
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))"
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 ]
small improvement to the previous answer.
>dec is polyfilled since it is a newer addition.
Pip, 8 bytes
L3Ya.#yy
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
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"
Perl, 31 bytes
Just going through all the numbers until one fits:
perl -lpe '1while++$n<length$_.$n;$_.=$n'
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
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;};
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!@
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.
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).
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
Perl 6, 46 35 bytes
{$_~(.chars,*.chars+.chars...{$^a==$^b})[*-1]}
{$_~(.chars,*.chars+.chars...*)[2]}
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,
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.
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
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`
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>