28
\$\begingroup\$

Input

A list of words separated by any number of spaces.

Output

A horizontal ASCII art graph, where the n-th line is composed by as many asterisks (*) as the n-th word is long.

Example usage

The > signals user input, you should not input it when testing the program.

> This is an example histogram of word length
****
**
**
*******
*********
**
****
******

> a aa aaa aaaa aaaaa
*
**
***
****
*****

> double space  example
******
*****
*******

Reference implementation

In case of doubt over the specification, the output of your program should match exactly that of the below program under all inputs.

puts gets.chomp.split.map{|word| '*' * word.length}.join("\n")
\$\endgroup\$
12
  • \$\begingroup\$ So is a trailing newline allowed? crosses fingers \$\endgroup\$
    – Beta Decay
    Sep 7, 2015 at 14:15
  • \$\begingroup\$ @BetaDecay Yes, allowed ........... \$\endgroup\$
    – Caridorc
    Sep 7, 2015 at 15:10
  • \$\begingroup\$ Will the input ever have leading or trailing spaces? \$\endgroup\$
    – PhiNotPi
    Sep 7, 2015 at 15:20
  • 8
    \$\begingroup\$ What you're describing is not a histogram. A histogram would show the number of words with x characters on line x. In the first example, line 1 would have 0 asterisks (no words with length 1) line 2 would have 3 asterisks (is, an, of) and so on. \$\endgroup\$
    – nitro2k01
    Sep 7, 2015 at 20:50
  • 1
    \$\begingroup\$ Ok, I realize you're right. Horizontal it is. \$\endgroup\$
    – nitro2k01
    Sep 7, 2015 at 21:08

34 Answers 34

1
2
1
\$\begingroup\$

AWK

 awk '{for(i=1;i<=NF;i++){while(k++<length($i)){printf "*"};k=0;print ""}}'

examples

 echo "this is programming" | awk '{for(i=1;i<=NF;i++){while(k++<length($i)){printf "*"};k=0;print ""}}'

output:-

****
**
***********
\$\endgroup\$
0
1
\$\begingroup\$

Ruby, 29 bytes

puts gets.gsub(/\S/,?*).split
\$\endgroup\$
1
\$\begingroup\$

Bash - 60 26 bytes

new solution (thanks manatwork):

for w;{ echo "${w//?/*}";}

old solution:

for w;do for((i=1;i<=${#w};i++));do printf \*;done;echo;done

~ $ ./l This is an example histogram of word length
****
**
**
*******
*********
**
****
******

The only part that might deserve an explanation is ${#w}: it returns the length of the string w.

\$\endgroup\$
2
  • \$\begingroup\$ Processing separate parameters instead of a single input to avoid completing half of the task, is kind of cheating. Anyway, what you wrote there, can be accomplished in 26 characters: for w;{ echo "${w//?/*}";} \$\endgroup\$
    – manatwork
    Sep 9, 2015 at 15:12
  • \$\begingroup\$ Yeah, I realized this was basically a character substitution just after posting that, so I posted another one in sed: codegolf.stackexchange.com/a/57435/7176 \$\endgroup\$
    – 1ace
    Sep 9, 2015 at 15:15
1
\$\begingroup\$

sed - 20 bytes

s/[^ ]/*/g;s/ +/\n/g

~ $ sed -re 's/[^ ]/*/g;s/ +/\n/g' <<< 'This is an example histogram of word   length'
****
**
**
*******
*********
**
****
******
\$\endgroup\$
1
  • \$\begingroup\$ @jimmy23013: I edited my answer accordingly, and fixed the multiple space issue :) \$\endgroup\$
    – 1ace
    Sep 10, 2015 at 9:12
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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