Repeat a string: Difference between revisions
(Added Scala) |
m (Fixed lang tags.) |
||
Line 3: | Line 3: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
In [[Ada]] multiplication of an universal integer to string gives the desired result. Here is an example of use: |
In [[Ada]] multiplication of an universal integer to string gives the desired result. Here is an example of use: |
||
⚫ | |||
⚫ | |||
⚫ | |||
with Ada.Text_IO; use Ada.Text_IO; |
with Ada.Text_IO; use Ada.Text_IO; |
||
Line 10: | Line 9: | ||
begin |
begin |
||
Put_Line (5 * "ha"); |
Put_Line (5 * "ha"); |
||
end String_Multiplication; |
end String_Multiplication;</lang> |
||
⚫ | |||
Sample output: |
Sample output: |
||
<pre> |
<pre> |
||
Line 17: | Line 15: | ||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<lang c> |
<lang c>#include <stdio.h> |
||
#include <stdio.h> |
|||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 35: | Line 32: | ||
int main() { |
int main() { |
||
printf("%s\n", string_repeat(5, "ha")); |
printf("%s\n", string_repeat(5, "ha")); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 60: | Line 55: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<lang |
<lang lisp>(apply str (apply concat (repeat 5 "ha")))</lang> |
||
=={{header|E}}== |
=={{header|E}}== |
||
Line 66: | Line 61: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth> |
<lang forth>: place-n { src len dest n -- } |
||
: place-n { src len dest n -- } |
|||
0 dest c! |
0 dest c! |
||
n 0 ?do src len dest +place loop ; |
n 0 ?do src len dest +place loop ; |
||
Line 73: | Line 67: | ||
create test 256 allot |
create test 256 allot |
||
s" ha" test 5 place-n |
s" ha" test 5 place-n |
||
test count type \ hahahahaha |
test count type \ hahahahaha</lang> |
||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 96: | Line 89: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<lang j> |
<lang j> 5 ((* #) $ ]) 'ha' |
||
⚫ | |||
5 ((* #) $ ]) 'ha' |
|||
hahahahaha |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 129: | Line 120: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<lang logo> |
<lang logo>to copies :n :thing [:acc "||] |
||
to copies :n :thing [:acc "||] |
|||
if :n = 0 [output :acc] |
if :n = 0 [output :acc] |
||
output (copies :n-1 :thing combine :acc :thing) |
output (copies :n-1 :thing combine :acc :thing) |
||
end |
end</lang> |
||
</lang> |
|||
or using cascade: |
or using cascade: |
||
⚫ | |||
<lang logo> |
|||
⚫ | |||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 184: | Line 171: | ||
repeating it more than 0 times results in the concatenation of the string and (n-1) further repeats. |
repeating it more than 0 times results in the concatenation of the string and (n-1) further repeats. |
||
<lang pure> |
<lang pure>> str_repeat 0 s = ""; |
||
> str_repeat 0 s = ""; |
|||
> str_repeat n s = s + (str_repeat (n-1) s) if n>0; |
> str_repeat n s = s + (str_repeat (n-1) s) if n>0; |
||
> str_repeat 5 "ha"; |
> str_repeat 5 "ha"; |
||
"hahahahaha" |
"hahahahaha" |
||
⚫ | |||
> |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 20:55, 21 November 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Take a string and repeat it some number of times. Example: repeat("ha", 5) => "hahahahaha"
Ada
In Ada multiplication of an universal integer to string gives the desired result. Here is an example of use: <lang Ada>with Ada.Strings.Fixed; use Ada.Strings.Fixed; with Ada.Text_IO; use Ada.Text_IO;
procedure String_Multiplication is begin
Put_Line (5 * "ha");
end String_Multiplication;</lang> Sample output:
hahahahaha
C
<lang c>#include <stdio.h>
- include <stdlib.h>
- include <string.h>
char * string_repeat( int n, const char * s ) {
size_t slen = strlen(s); char * dest = (char *)calloc(n*slen, sizeof(char));
int i; char * p; for ( i=0, p = dest; i < n; ++i, p += slen ) { memcpy(p, s, slen); } return dest;
}
int main() {
printf("%s\n", string_repeat(5, "ha"));
}</lang>
C++
<lang cpp>#include <string>
- include <iostream>
std::string repeat( const std::string &word, int times ) {
std::string result ; result.reserve(times*word.length()); // avoid repeated reallocation for ( int a = 0 ; a < times ; a++ ) result += word ; return result ;
}
int main( ) {
std::cout << repeat( "Ha" , 5 ) << std::endl ; return 0 ;
}</lang>
C#
<lang csharp> String s = "".PadLeft(5, 'X').Replace("X", "ha")</lang>
Clojure
<lang lisp>(apply str (apply concat (repeat 5 "ha")))</lang>
E
<lang e>"ha" * 5</lang>
Forth
<lang forth>: place-n { src len dest n -- }
0 dest c! n 0 ?do src len dest +place loop ;
create test 256 allot s" ha" test 5 place-n test count type \ hahahahaha</lang>
Fortran
<lang fortran>program test_repeat
write (*, '(a)') repeat ('ha', 5)
end program test_repeat</lang> Output:
hahahahaha
Haskell
For a string of finite length:
<lang haskell>concat $ replicate 5 "ha"</lang>
For an infinitely long string:
<lang haskell>cycle "ha"</lang>
J
<lang j> 5 ((* #) $ ]) 'ha' hahahahaha</lang>
Java
There's no function or operator to do this in Java, so you have to do it yourself. <lang java5>public static String repeat(String str, int times){
StringBuilder ret = new StringBuilder(); for(int i = 0;i < times;i++) ret.append(str); return ret.toString();
}
public static void main(String[] args){
System.out.println(repeat("ha", 5));
}</lang>
Or even shorter: <lang java5>public static String repeat(String str, int times){
return new String(new char[times]).replace("\0", str);
}</lang>
JavaScript
This solution creates an array of n+1 null elements, then joins them using the target string as the delimiter <lang javascript>String.prototype.repeat = function(n) {
return new Array(1 + parseInt(n, 10)).join(this);
}
alert("ha".repeat(5)); // hahahahaha</lang>
Logo
<lang logo>to copies :n :thing [:acc "||]
if :n = 0 [output :acc] output (copies :n-1 :thing combine :acc :thing)
end</lang> or using cascade: <lang logo>show cascade 5 [combine "ha ?] "|| ; hahahahaha</lang>
OCaml
<lang ocaml>let string_repeat s n =
let len = String.length s in let res = String.create(n * len) in for i = 0 to pred n do String.blit s 0 res (i * len) len; done; (res)
- </lang>
testing in the toplevel: <lang ocaml># string_repeat "Hiuoa" 3 ;; - : string = "HiuoaHiuoaHiuoa"</lang>
Alternately: <lang ocaml>let string_repeat s n =
String.concat "" (Array.to_list (Array.make n s))
- </lang>
Or: <lang ocaml>let string_repeat s n =
Array.fold_left (^) "" (Array.make n s)
- </lang>
Perl
<lang perl>"ha" x 5</lang>
Perl 6
<lang perl6>"ha" x 5</lang>
(Note that the x
operator isn't quite the same as in Perl 5: it now only creates strings. To create lists, use xx
.)
PHP
<lang php>str_repeat("ha", 5)</lang>
PowerBASIC
<lang powerbasic>MSGBOX REPEAT$(5, "ha")</lang>
Pure
str_repeat is defined by pattern-matching: repeating any string 0 times results in the empty string; while repeating it more than 0 times results in the concatenation of the string and (n-1) further repeats.
<lang pure>> str_repeat 0 s = ""; > str_repeat n s = s + (str_repeat (n-1) s) if n>0; > str_repeat 5 "ha"; "hahahahaha" ></lang>
Python
<lang python>"ha" * 5 # ==> "hahahahaha"</lang>
Ruby
<lang ruby>"ha" * 5 # ==> "hahahahaha"</lang>
Scala
<lang scala>"ha" * 5 // ==> "hahahahaha"</lang>
Tcl
<lang tcl>string repeat "ha" 5 ;# => hahahahaha</lang>