| Bytes | Lang | Time | Link |
|---|---|---|---|
| 022 | Tcl | 180610T231706Z | sergiol |
| 012 | Thing | 180612T072333Z | Windmill |
| 005 | Braingolf | 180611T105800Z | Mayube |
| 002 | RProgN 2 | 180611T085625Z | ATaco |
| 002 | Japt | 180611T085419Z | Shaggy |
| 002 | 05AB1E | 180611T083447Z | Kevin Cr |
| 022 | Python 2 | 160906T101336Z | Erik the |
| 025 | R | 160906T112035Z | cia_rana |
| 067 | Go | 160906T110827Z | cia_rana |
| 028 | Java 8 | 160905T121744Z | Shaun Wi |
| 025 | PowerShell | 140128T170722Z | microbia |
| 057 | C | 140128T174046Z | Josh |
| 004 | APL | 140130T031501Z | Gandalf |
| 001 | GolfScript | 140126T153500Z | Doorknob |
| 030 | PHP | 140130T000358Z | Flame |
| nan | 140128T231812Z | shemerey | |
| nan | I'm trying to stay away from the simple string length functions | 140126T164631Z | Tyzoid |
| nan | 140128T170156Z | draegtun | |
| 019 | bash | 140128T200128Z | user1525 |
| nan | 140128T194006Z | bela | |
| 003 | GTB | 140128T160502Z | Timtech |
| nan | 140128T153827Z | plannapu | |
| 059 | C | 140128T145028Z | Oberon |
| 025 | python 2 | 140126T155449Z | Wasi |
| nan | 140128T132238Z | glezmen | |
| 812 | Befunge98 | 140126T170235Z | FireFly |
| 020 | JavaScript | 140126T162742Z | Blue She |
| 003 | Dc | 140126T163537Z | manatwor |
| nan | Perl with lp 11 bytes | 140126T163126Z | null |
| 007 | k [7 chars] | 140126T162757Z | nyi |
Thing, 12 bytes
idh\-=[tLe]L
How?
Gets input from command line arguments. If the first character of input is "-", push the length of the all but first items of the string and immediately end the program. Else, push the length of the string. Then implicitly print.
Does not work with multiline.
i push input
d duplicate
h first character of
\- push the character "-"
= if top two values are equal, push 1, if not, 0
[ if top value is truthy (not [] or 0), go into the block. if not, go past the end of the block.
t push the all but first items of the stack's top value (literally s[1:] in python)
L length
e end the program
] end loop
L length
05AB1E, 2 bytes
þg
Try it online or verify all test cases.
Explanation:
þ # Take the digits of the input
g # And output the length
Python 2, 22 bytes
lambda a:len(`abs(a)`)
I actually built this myself, before seeing Wasi's answer! This is how I thought of it:
- I need: a
lambda. len?False! What aboutn<0? Should the-be counted?False!- How to eliminate the
-in just a few bytes?abs!
[Thinking ends]
R, 25 bytes
cat(nchar(scan("stdin")))
Go, 67 bytes
package main;import."fmt";func main(){s:="";Scan(&s);Print(len(s))}
Java 8, 29 28 bytes
i->(""+(i<0?-i:i)).length();
Ungolfed test program
public static void main(String[] args) {
IntFunction<Integer> func = i -> ("" + (i < 0 ? -i : i)).length();
System.out.println(func.apply(12345)); //5
}
PowerShell (25)
($n-replace"-",'').length
where $n is a number
C, 63 59 57 characters
It's slightly longer than the other C solution, but it works on arbitrarily large numbers (numbers with 2^32 - 1 digits rather than numbers up to 2^32 - 1).
EDIT: It's now shorter than the other C solution!
c;main(n){n-10?main(getchar(),c+=n>47):printf("%d\n",c);}
Note: if you call the program with exactly 9 arguments, it will fail. But why would you do that??
APL - 4 characters
ρ'n'
The first symbol, rho, checks the number of cells in an array, where one character of a string is equal to one cell.
GolfScript, 1 character
,
, means length...
Edit: You seem to have changed your question... please don't do that, but I still have a solution:
GolfScript, 12 7 characters (for multiline)
n%{,n}/
Thanks @Howard for reminding me about n to push newline and using separate strings instead of one, to save 5 chars ;)
Solutions that work with negative numbers:
GolfScript, 5 (for negative)
'-'-,
GolfScript, 11 (for multiline and negative)
n%{'-'-,n}/
PHP (30 chars)
echo $n<0?(strlen($n)-1):strlen($n)
(without php opening tags <?php & ?>)
Explanation
Pretty simple, ints can be interpreted as strings, therefore $n<0 has a minus-char that should be removed in the true-statement of the ternary operator. If its positive then we just count the length ;)
Ruby
$*.first.size
or
ARGV.first.size
I'm trying to stay away from the simple string length functions, but the BF solution basically does a count.
Pure math/Mathematica - 22 chars (for mathematica)
floor(log10(abs(n)))+1
Thanks to @plg for the correction when n is a power of 10. This invokes undefined behavior when n=0, but that case can be returned by a simple check.
PHP implementation of the above math solution - 34 chars
($n==0)?1:floor(log(abs($n),10))+1;
BrainF*ck - 54 characters
>>+[<+>++++++++++,----------]<-<++++++++[>++++++<-]>.
Perl
15 chars - no input or constraint checks
$_=()=$_=~/\d/g
48 chars - checks input & constraint
$_=/^-?(\d+)$/?($1<=10**9?length($1)."\n":$_):$_
NB. If the input ($_) is an integer (positive or negative) and comes under the constraint then it amends $_ to the number of digits. Otherwise it just leaves the input as is.
Usage example:
perl -ple '... insert above code...'
bash - 19 characters
Assuming input via standard input:
grep -o [0-9]|wc -l
Multi-line version - 48 characters:
while read L;do echo $L|grep -o [0-9]|wc -l;done
A DELPHI PROGRAM FOR CONSOLE
program P;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
var a: Integer;
begin
readln(a);
writeln(length(IntToStr(a)));
end.
GTB, 3
Assuming value is already stored:
l?_
To make user input:
`_l?_
Newline support with interactive input:
[`_~l?_]
R
The solution at 18 characters (identical in concept to the python solution of Wasi):
nchar(abs(scan()))
And the funnier one at 33 characters:
which(!abs(scan())%/%10^(1:9))[1]
C - 59 characters
main(k){scanf("%i",&k);printf("%i\n",(int)log10(abs(k))+1);}
python 2, 25
print len(`abs(input())`)
works for both positive and negative integers
Perl
s/\D//;$_=length
run as:
perl -ple 's/\D//;$_=length'
Befunge-98, 8 or 12 chars
Single number (actually counts input bytes ≥15 and prints count at EOF)#.~f`+#@
Multiline support (same as above, but print count at byte <15 and reset counter)
#@~f`:2*j\.+
JavaScript, 20
alert((""+n).length)
Dc: 3 characters
?Zp
Sample run:
bash-4.2$ dc -e '?Zp' <<< '12346'
5
bash-4.2$ dc -e '?Zp' <<< '345'
3
Perl (with -lp) (11 bytes, counting lp)
$_=length
k [7 chars]
f:{#$x}
Example:
f 12346
5
f 456
3
Though only 2 characters (#$) are required to find the length of the input number.
#$123444
6
Explanation
convert the input to string.
$123456
"123456"
count the number of characters:
#$123456
6
Q equivalent of this code:
f:{count string x}
f 123456
6