Repeat a string: Difference between revisions

 
(15 intermediate revisions by 12 users not shown)
Line 156:
even</syntaxhighlight>
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="asm8080"> org 100h
jmp demo
 
; Repeat the string at DE into HL, B times
repeat: mvi c,'$' ; string terminator
xra a ; repeat 0x?
ora b
mov m,c ; then empty string
rz
rpt1: push d ; save begin of string to repeat
chcpy: ldax d ; copy character from input to output
mov m,a
inx d ; advance pointers
inx h
cmp c ; end of string?
jnz chcpy
pop d ; restore begin of string to repeat
dcx h ; move back past terminator in copy
dcr b ; done yet?
jnz rpt1 ; if not add another copy
ret
 
demo: lxi d,ha ; get string to repeat
lxi h,buf ; place to store result
mvi b,5 ; repeat 5 times
call repeat
lxi d,buf ; print result using CP/M call
mvi c,9
jmp 5
 
ha: db 'ha$' ; string to repeat
buf: ds 32 ; place to store repeated string</syntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
=={{header|8th}}==
<syntaxhighlight lang="forth">"ha" 5 s:*
Line 382 ⟶ 417:
print StringRepeat$("*", 5)
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 print stringrepeat$("rosetta",1)
120 print stringrepeat$("ha",5)
130 print stringrepeat$("*",5)
140 end
150 function stringrepeat$(s$,n)
160 cad$ = ""
170 for i = 1 to n
180 cad$ = cad$+s$
190 next i
200 stringrepeat$ = cad$
210 end function</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 CLS : rem 100 HOME for Applesoft BASIC
110 S$ = "rosetta" : N = 1
120 GOSUB 210
130 PRINT CAD$
140 S$ = "ha" : N = 5
150 GOSUB 210
160 PRINT CAD$
170 S$ = "*" : N = 5
180 GOSUB 210
190 PRINT CAD$
200 END
210 REM FUNCTION STRINGREPEAT$(S$,N)
220 CAD$ = ""
230 FOR I = 1 TO N
240 CAD$ = CAD$+S$
250 NEXT I
260 RETURN</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">100 REM Repeat a string
110 LET S$ = "rosetta"
120 LET N = 1
130 GOSUB 210
140 LET S$ = "ha"
150 LET N = 5
160 GOSUB 210
170 LET S$ = "*"
180 LET N = 5
190 GOSUB 210
200 STOP
210 REM FUNCTION StringRepeat$(S$,N)
220 FOR I = 1 TO N
230 PRINT S$;
240 NEXT I
250 PRINT
260 RETURN
270 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
Line 396 ⟶ 496:
PRINT StringRepeat$("*", 5)
END</syntaxhighlight>
 
==={{header|Quite BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 LET S$ = "rosetta"
115 LET N = 1
120 GOSUB 210
130 PRINT C$
140 LET S$ = "ha"
145 LET N = 5
150 GOSUB 210
160 PRINT C$
170 LET S$ = "*"
175 LET N = 5
180 GOSUB 210
190 PRINT C$
200 END
210 REM FUNCTION STRINGREPEAT$(S$,N)
220 LET C$ = ""
230 FOR I = 1 TO N
240 LET C$ = C$ + S$
250 NEXT I
260 RETURN</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 410 ⟶ 538:
PRINT StringRepeat$("*", 5)
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Repeat a string"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION StringRepeat$ (s$, n)
 
FUNCTION Entry ()
 
PRINT StringRepeat$ ("rosetta", 1)
PRINT StringRepeat$ ("ha", 5)
PRINT StringRepeat$ ("*", 5)
 
END FUNCTION
FUNCTION StringRepeat$ (s$, n)
cad$ = ""
FOR i = 1 TO n
cad$ = cad$ + s$
NEXT i
RETURN cad$
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 830 ⟶ 982:
=={{header|E}}==
<syntaxhighlight lang="e">"ha" * 5</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
func$ rep s$ n .
for i to n
r$ &= s$
.
return r$
.
print rep "ha" 5
</syntaxhighlight>
 
=={{header|ECL}}==
Line 872 ⟶ 1,035:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 879 ⟶ 1,042:
public program()
{
var s := new Range(0, 5).selectBy::(x => "ha").summarize(new StringWriter())
}</syntaxhighlight>
 
Line 1,214 ⟶ 1,377:
say "ha" repeated 5 times;
end the story.</syntaxhighlight>
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(str* "ha" 5)
</syntaxhighlight>
 
{{out}}
 
<pre>
hahahahaha
</pre>
 
=={{header|IS-BASIC}}==
Line 1,236 ⟶ 1,411:
 
=={{header|Java}}==
There are a few ways to achieve this in Java.<br />
Starting with Java 11 you can use the ''String.repeat'' method.
<syntaxhighlight lang="java">
"ha".repeat(5);
</syntaxhighlight>
Which, if you view its implementation, is just using the ''Arrays.fill'' method.
<syntaxhighlight lang="java">
String[] strings = new String[5];
Arrays.fill(strings, "ha");
StringBuilder repeated = new StringBuilder();
for (String string : strings)
repeated.append(string);
</syntaxhighlight>
And if you look at the 'Arrays.fill' implementation, it's just a for-loop, which is likely the most idiomatic approach.
<syntaxhighlight lang="java">
String string = "ha";
StringBuilder repeated = new StringBuilder();
int count = 5;
while (count-- > 0)
repeated.append(string);
</syntaxhighlight>
<br />
Or
{{works with|Java|1.5+}}
 
There'sBefore Java 11 there was no method or operator to do this in Java, so you havehad to do it yourself.
 
<syntaxhighlight lang="java5">public static String repeat(String str, int times) {
Line 1,329 ⟶ 1,527:
{{Out}}
<pre>hahahahaha</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE repeat == "" rotate [concat] cons times.
 
"ha" 5 repeat.</syntaxhighlight>
 
=={{header|jq}}==
<syntaxhighlight lang="jq">"a " * 3 # => "a a a "</syntaxhighlight>
 
Note that if the integer multiplicand is 0, then the result is the JSON value '''null'''.
 
Line 1,363 ⟶ 1,565:
 
=={{header|LabVIEW}}==
I don't know if there is a built-in function for this, but it is easily achieved with a For loop and Concatenate Strings.<br/>
[[file:LabVIEW_Repeat_a_string.png]]
<br/>
By using built in functions:
 
[[File:Panel.png]]
[[File:BlockDiagram.png]]
<br/>
 
=={{header|Lambdatalk}}==
Line 1,390 ⟶ 1,598:
{repeat ha 10}
-> hahahahahahahahahahaha
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
# Repeat text function
fn.println(fn.repeatText(5, ha))
# Output: hahahahaha
 
# Mul operator
fn.println(parser.op(ha * 5))
# Output: hahahahaha
 
# Mul operator function
fn.println(fn.mul(ha, 5))
# Output: hahahahaha
</syntaxhighlight>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">"ha" * 5</syntaxhighlight>
This example looks like Perl, but the x operator doesn't just multiply strings in langur.
<syntaxhighlight lang="langur">"ha" x 5</syntaxhighlight>
 
=={{header|Lasso}}==
Line 1,500 ⟶ 1,722:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">(*StringRepeat["ha", solution 1 *)5]</syntaxhighlight>
rep[n_Integer,s_String]:=Apply[StringJoin,ConstantArray[s,{n}]]
(* solution 2 -- @@ is the infix form of Apply[] *)
rep[n_Integer,s_String]:=StringJoin@@Table[s,{n}]
(* solution 3 -- demonstrating another of the large number of looping constructs available *)
rep[n_Integer,s_String]:=Nest[StringJoin[s, #] &,s,n-1]</syntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 2,339 ⟶ 2,556:
<syntaxhighlight lang="ring"> Copy("ha" , 5) # ==> "hahahahaha"</syntaxhighlight>
 
=={{header|RPL}}==
≪ "" 1 5 START "ha" + NEXT ≫ EVAL
{{out}}
<pre>1: "hahahahaha"</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">"ha" * 5 # ==> "hahahahaha"</syntaxhighlight>
Line 2,859 ⟶ 3,080:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">System.print("ha" * 5)</syntaxhighlight>
 
{{out}}
885

edits