Repeat a string

Revision as of 11:30, 7 November 2009 by rosettacode>Dmitry-kazakov (Ada solution added)

Take a string and repeat it some number of times. Example: repeat("ha", 5) => "hahahahaha"

Task
Repeat a string
You are encouraged to solve this task according to the task description, using any language you may know.

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>

  1. include <stdio.h>
  2. include <stdlib.h>
  3. 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>

Clojure

<lang clojure>(apply str (apply concat (repeat 5 "ha")))</lang>

E

<lang e>"ha" * 5</lang>

Haskell

<lang haskell>concat $ replicate 5 "ha"</lang>

J

<lang j>

  5 ((* #) $ ]) 'ha'

hahahahaha </lang>

Java

Works with: Java version 1.5+

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>

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>

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

Works with: Rakudo version #21 "Seattle"

<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>

Tcl

<lang tcl>string repeat "ha" 5  ;# => hahahahaha</lang>