Bash - 181 158 chars
Edit 1: I used this trick for the for
loop, remove whitespace, condence an if
statement
n=0;for ((i=0;i<${#1};i++));{ c="${1:$i:1}";if [ $c = '0' ]; then ((n++)); else [ $n -ne 0 ] && { echo -n $n;n=0;};echo -n $c;fi;};[ $n -ne 0 ] && echo -n $n
Explanation
# a counter of how far into a sequence of zeros we are
# this is zero if we're not in a 'zero-sequence'
n=0
# iterate over all the characters
# $1 is the first function argument ($0 would be the script name)
for (( i=0; i<${#1}; i++ )); do
# get the current char
c="${1:$i:1}"
# if it is a zero, then increment the counter
if [ $c = '0' ]; then
((n++));
# if it is NOT a zero
else
# if a zero sequence is over (given that the counter is
# not equal to zero and the current char isn't zero)
if [ $n -ne 0 ]; then
# print the number of zeros in the sequence,
# and reset the counter to zero
echo -n $n;
n=0;
fi;
echo -n $c;
fi
done
# check if there was a zero sequence terminating the string,
# as we wouldn't otherwise check as there wouldn't be a non-zero
# char initiating the check
if [ $n -ne 0 ]; then echo -n $n; fi
I referred to this SO answer for how to iterate over a string in Bash and this AU answer for a short way to increment a variable
0
s replaced with?11
? \$\endgroup\$10
as no modifications to the output were also needed, as most answers suggest \$\endgroup\$