Repeat a string: Difference between revisions
Content added Content deleted
(→{{header|Clojure}}: add Clojure example) |
(→{{header|C}}: C implementation) |
||
Line 1: | Line 1: | ||
{{task|String manipulation}}Take a string and repeat it some number of times. Example: repeat("ha", 5) => "hahahahaha" |
{{task|String manipulation}}Take a string and repeat it some number of times. Example: repeat("ha", 5) => "hahahahaha" |
||
=={{header|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> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |