Shortest program that calculates the length of a number [duplicate]

Given a number between 0 and 999999999 and output its length.

This is what I managed to work out (scoring 36):

s;main(){putchar(48+strlen(gets(&s)));}


Requirements:

• Take ONE input string (with a trailing newline, LF) from STDIN.
• Output a number (between 0 and 9, see notes below) to STDOUT.
A trailing newline does not matter.

Bonuses:

1. Takes input that is not numbers (e.g. abcdefg and output 7) but printable ASCII characters instead. The only newline LF is the last one. No CR. Beware of spaces and tabs. Reward: -3 bytes in score
2. Takes input of a greater length. Reward: -3 bytes if 9 < max length < 232, -6 bytes if max length >= 232. Practical compiler/interpreter must exist.
3. Complete both 1 and 2 for an extra -3 bytes.

Please note in your answer if you qualify for any bonuses.

• Anyone comes up with a C program shorter than 39? – iBug Jul 4 '17 at 10:48
• Bonuses are generally frowned upon in this community. – Okx Jul 4 '17 at 11:18
• I would recommend that you don't include bonus 1. That makes it no different and no more difficult than 'calculate the length of a string'. However, it's a bit too late to do this now; you should use the sandbox in the future. – MD XF Jul 31 '17 at 18:28

Python 3, 3 - 12 = -9 bytes

Qualifies for all bonuses

len


Try it online!

Mathematica, 12-12=0

StringLength


brainfuck, 9 - 3 = 6 bytes

,[>+<,]>.


Qualifies string bonus. Outputs as a character code.

Try it online!

• Better if in readable number. – iBug Jul 4 '17 at 10:53
• Brainfuck has upper limit 255. – Erik the Outgolfer Jul 4 '17 at 10:53
• @EriktheOutgolfer Ah, I'm stupid :P – Okx Jul 4 '17 at 10:55

Braingolf, 2 - 6 = -4 bytes

dl


Try it online!

Only works if input is entirely numerical

Or:

Braingolf, 1 - 12 = -11 bytes

l


Try it online!

Only works if input contains at least one non-numerical character

05AB1E, 1 - 12 = -11 bytes

g


Qualifies for the first bonus and the second, and then another -3 bytes.

Try it online!

• Actually -12 bytes. – Erik the Outgolfer Jul 4 '17 at 10:41
• @EriktheOutgolfer Actually -12 bytes (-3 bytes if you do both bonuses) – Okx Jul 4 '17 at 10:42
• Erm, that's what I said isn't it? – Erik the Outgolfer Jul 4 '17 at 10:43
• How is 1 - 3 - 6 - 3 = -12 and not -11? – Adám Jul 4 '17 at 10:45
• @Adám I can do maths :P – Okx Jul 4 '17 at 10:45

Neim, 1 -6 = -5 bytes

𝐥


Builtin that gets the length. A trailing newline can be added to the input, and will not affect the output. -6 bytes because it can accept any length.

Try it online!

Retina, 1 - 9 = -8 bytes

Qualifies for the first bonus and for the first part of the second bonus

.


Try it online!

APL (Dyalog) 64 bit, 1 - 12 = -11 bytes

≢


Try it online!

Windows batch, 53 - 3 - 3 - 3 = 44 bytes

@echo %1>f
@for %%G in (f)do @set/ar=%%G-2
@echo %r%


Redirect input to file, that get file length without CR\LF.

Japt, 1-6= -5 bytes

Qualifies for the first bonus and the first part of the second bonus. Theoretically it should qualify for the second part too as the maximum length of a string in JavaScript is 2**53-1 but I'm not aware of any browser that can handle anything longer than ~2**30.

l


Test it

• if it meets the -6 length bonus AND the -3 non-numerical bonus, you get another -3, so it's 1 - 12 = -11 – Skidsdev Jul 4 '17 at 10:54
• @Mayube, strings in JS can be up to 2**53-1 characters in length but I don't know if there's any browser that can handle strings longer than ~2**30 - I'm looking into it at the moment. – Shaggy Jul 4 '17 at 10:57

JavaScript (ES6), 11-6= 5 bytes

Qualifies for the first bonus and the first part of the second bonus. Theoretically it should qualify for the second part too as the maximum length of a string in JavaScript is 2**53-1 but I'm not aware of any browser that can handle anything longer than ~2**30.

s=>s.length


Pyth, 2 - 3 - 6 - 3 = -10 (?) bytes

I think it qualifies for all, because Pyth is python-based:

lw


My first Pyth submission. Hooray!

Try it online!

Explanation

lw     Full program: Get length of raw_input

l      Get length (string)
w     Take raw input


Lua, 10 - 3 - 3 - 3 = 1 bytes

string.len


I couldn't get lua to work with more than 2^31 length strings. If I manage to do that, I'll update here.

Try it online!

Pari/GP, 5 bytes

s->#s


Try it online!

dc, 2 - 12 = -10 bytes

Zn


Takes length of TOS and prints it. Note that strings must be surrounded by [].

TI-Basic, 2-6= -4 bytes

log(Ans


Input must be numerical and less than 2^100