Jump to content

Repeat a string: Difference between revisions

→‎{{header|C}}: C implementation
(→‎{{header|Clojure}}: add Clojure example)
(→‎{{header|C}}: C implementation)
Line 1:
{{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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.