Repeat a string: Difference between revisions

Content added Content deleted
(Add Racket examples)
(→‎{{header|Forth}}: fix bizarre formatting... oh wait, this code is obviously wrong. Funny how that works.)
Line 334: Line 334:
n 0 ?do src len dest +place loop ;
n 0 ?do src len dest +place loop ;


s" ha" pad 5 place-n
create test 256 allot
pad count type \ hahahahaha</lang>
s" ha" test 5 place-n
test count type \ hahahahaha</lang>
The same code without the use of locals:
The same code without the use of locals:
<lang forth> ( src len dest n -- )
<lang forth>
: place-n 0 over c! 0 ?do >r 2dup r@ +place r> loop drop 2drop ;
: place-n ( src len dest n -- )
swap >r 0 r@ c!
begin dup while -rot 2dup r@ +place rot 1- repeat
r> 2drop 2drop ;


s" ha" pad 5 place-n
create test 256 allot
pad count type \ hahahahaha</lang>
s" ha" test 5 place-n
test count type cr \ hahahahaha</lang>
Filling a string with a single character is supported by ANS-Forth:
Filling a string with a single character is supported by ANS-Forth:
<lang forth>create test 256 allot
<lang forth>pad 10 char * fill \ repeat a single character
pad 10 type \ **********</lang>

test 10 char * fill \ repeat a single character
test 10 type</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==