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.


Just a string of arbitrary length (can be empty).


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


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.

  • \$\begingroup\$ What characters can appear in the input? \$\endgroup\$ – Martin Ender Dec 16 '16 at 21:48
  • \$\begingroup\$ @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. \$\endgroup\$ – Yodle Dec 16 '16 at 22:42

40 Answers 40


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.


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


$ awk '{FS="";r=$0;$0=r NF;$0=r NF;$0=r NF}1' file
| improve this answer | |

Jelly, 12 bytes

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



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.
| improve this answer | |

Perl, 31 bytes

Just going through all the numbers until one fits:

perl -lpe '1while++$n<length$_.$n;$_.=$n'
| improve this answer | |

Ruby, 43 36 bytes

Anonymous function, abusing the $. global.


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"
| improve this answer | |

C, 75 bytes

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



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.

| improve this answer | |
  • \$\begingroup\$ I don't believe this method works for 98 or 997 \$\endgroup\$ – Blue Dec 17 '16 at 16:37

Under review

Powershell V2.0, 64 bytes



PS C:\Users\***\Downloads\golf> .\str-length.ps1 'This is my strin3333333333333333333333333333333333333333333333333333
This is my strin333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333g113
| improve this answer | |
  • \$\begingroup\$ This outputs aaaaaaaaa10 when given input aaaaaaaaa, and not the correct aaaaaaaaa11. \$\endgroup\$ – AdmBorkBork Dec 19 '16 at 16:34
  • \$\begingroup\$ You're right, I missed that somehow. \$\endgroup\$ – 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
| improve this answer | |

awk, 27 bytes


Test it:

$ awk -F '' '$0=$0length($0length($0NF))' file
| improve this answer | |
  • 1
    \$\begingroup\$ You can change length($0) to just length for brevity. \$\endgroup\$ – Ed Morton Dec 23 '16 at 16:40
  • \$\begingroup\$ @EdMorton Been experimenting with awk -F '' '$0=NF' \$\endgroup\$ – James Brown Dec 24 '16 at 13:46

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


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

    // wrap the loop in a try/catch
        // 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;
| improve this answer | |

Java 7, 67 bytes

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

Try it online!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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