><>, 38 37 Bytes
Curse you double space case *shakes fish*.
<v&0
>i:84*=?v0(?;67*o&1&
\ &0o?&a/
You can try it online (all you need to do is give input through the field near the bottom and then hit the Give
button). Suggestions for further golfing are always welcome, especially ideas to remove those wasteful spaces in front of the second and third lines.
If you were allowed to print an additional newline for extra spaces, the code could be a whopping 27 bytes:
>i:84*=?v0(?;67*o
^ oa<
Explanation
Note: the order of the explanation will correspond to the pointer's location (so if the code is explained out of what one would consider order, it is because it is the order in which the pointer executes it).
Line 1:
<v&0
< redirects flow leftward
0 pushes 0 onto the stack
& pops 0 and puts it in the register
v redirects flow downward
Line 2:
>i:84*=?v0(?;67*o&1&
> redirects flow leftward
i: pushes input and then duplicates it
84* pushes 32 (the space character numerically)
=?v pops 32 and input and redirects flow downward if they're equal
0(?; pops input and terminates if input is less than 0*
67*o pushes 42 (asterisk) and prints it
&1& pushes register value and then puts 1 in the register
*in ><>, the command i returns -1 if no input is given
Line 3:
N.B. This line goes in reverse, so read right to left.
^ &0o?&a<
< redirects flow leftward
a pushes 10 (newline) onto the stack
o?& prints a newline if the register is not 0
&0 sets the register to 0
^ redirects flow upwards (back to the second line)
Basically, the program test to make sure the input (which is read one character at a time) is not a space and then prints an asterisk. It terminates if there is no input (the input value is -1). To make sure it doesn't print additional newlines, it uses the register value, which it either sets to 0 or 1. Because of the way I set it up, it doesn't care about the extraneous values pushed onto the stack (e.g. the value of the register when it sets it to 1
after printing an asterisk); they remain on the stack when the program terminates but do nothing.
I know it might be a bit confusing since I used 84*
and 67*
instead of " "
and "*"
respectively, but that was because I didn't feel like putting strings in the program for whatever reason.