Rep-string: Difference between revisions

Add Forth
(Add Forth)
Line 418:
1 (no repeat)</pre>
 
=={{header|Forth}}==
{{trans|Python}}
<lang forth>: rep-string ( caddr1 u1 -- caddr2 u2 ) \ u2=0: not a rep-string
2dup dup >r r@ 2/ /string
begin 2over 2over string-prefix? 0= over r@ < and while -1 /string repeat
r> swap - >r 2drop r> ;
 
: test ( caddr u -- )
2dup type ." has "
rep-string ?dup 0= if drop ." no " else type ." as " then
." repeating substring" cr ;
: tests
s" 1001110011" test
s" 1110111011" test
s" 0010010010" test
s" 1010101010" test
s" 1111111111" test
s" 0100101101" test
s" 0100100" test
s" 101" test
s" 11" test
s" 00" test
s" 1" test ;</lang>
{{out}}
<lang forth>cr tests
1001110011 has 10011 as repeating substring
1110111011 has 1110 as repeating substring
0010010010 has 001 as repeating substring
1010101010 has 1010 as repeating substring
1111111111 has 11111 as repeating substring
0100101101 has no repeating substring
0100100 has 010 as repeating substring
101 has no repeating substring
11 has 1 as repeating substring
00 has 0 as repeating substring
1 has no repeating substring
ok</lang>
=={{header|Go}}==
{{trans|Python}}