String length: Difference between revisions

Content added Content deleted
m (→‎{{header|Kotlin}}: made the kotlin example not use java)
(→‎sed: de-obfuscate & simplify)
Line 3,029: Line 3,029:
Text is read from standard input e.g. <code>echo "string" | sed -f script.sed</code> or <code>sed -f script.sed file.txt</code> (The solution given would be the contents of a text file <code>script.sed</code> in these cases).
Text is read from standard input e.g. <code>echo "string" | sed -f script.sed</code> or <code>sed -f script.sed file.txt</code> (The solution given would be the contents of a text file <code>script.sed</code> in these cases).
For files with more than one line, sed will give a count for each line.
For files with more than one line, sed will give a count for each line.
<syntaxhighlight lang="sed"># create unary numeral (i = 1)
The 'convert to digits' section is based off of [http://unix.stackexchange.com/a/36959/11750 this StackExchange answer].
s/./i/g
<syntaxhighlight lang="sed"># Change all characters to '|'.
:loop
s/./\|/g;
# divide by 10 (x = 10)

s/i\{10\}/x/g
# Convert to digits
# convert remainder to decimal digit
:convert
/i/!s/[0-9]*$/0&/
s/||||||||||/</g
s/<\([0-9]*\)$/<0\1/g
s/i\{9\}/9/
s/|||||||||/9/g;
s/i\{8\}/8/
s/i\{7\}/7/
s/|||||||||/9/g; s/||||||||/8/g; s/|||||||/7/g; s/||||||/6/g;
s/i\{6\}/6/
s/|||||/5/g; s/||||/4/g; s/|||/3/g; s/||/2/g; s/|/1/g;
s/</|/g
s/iiiii/5/
s/iiii/4/
t convert
s/iii/3/
s/^$/0/</syntaxhighlight>
s/ii/2/
s/i/1/
# convert quotient (10s) to 1s
y/x/i/
# start over for the next magnitude (if any)
/i/b loop</syntaxhighlight>


=={{header|Seed7}}==
=={{header|Seed7}}==