# Appending String Lengths

### 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 , 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.

• What characters can appear in the input? – Martin Ender Dec 16 '16 at 21:48
• @MartinEnder Alphanumerical characters only, 0-9 and A-Z/a-z. So yes, you can have strings with numbers at the end. I'll add a test case for one. – Yodle Dec 16 '16 at 22:42

## 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  # 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.  # Perl, 31 bytes Just going through all the numbers until one fits: perl -lpe '1while++$n<length$_.$n;$_.=$n'


# 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"  # 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. • I don't believe this method works for 98 or 997 – Blue Dec 17 '16 at 16:37 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

• This outputs aaaaaaaaa10 when given input aaaaaaaaa, and not the correct aaaaaaaaa11. – AdmBorkBork Dec 19 '16 at 16:34
• You're right, I missed that somehow. – Kieron Davies Dec 20 '16 at 8:34

# Scala, 75 bytes

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


## awk, 27 bytes

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


Test it:

$awk -F '' '$0=$0length($0length($0NF))' file 7aaaaaa8 8aaaaaaa9 9aaaaaaaa11 97aaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa99 98aaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa101  • You can change length($0) to just length for brevity. – Ed Morton Dec 23 '16 at 16:40
• @EdMorton Been experimenting with awk -F '' '\$0=NF' – James Brown Dec 24 '16 at 13:46

# C# / CS Script 9987 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;
}


# Java 7, 67 bytes

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


Try it online!