Repeat a string: Difference between revisions
Content deleted Content added
No edit summary |
→{{header|C}}: add variation |
||
Line 39: | Line 39: | ||
printf("%s\n", string_repeat(5, "ha")); |
printf("%s\n", string_repeat(5, "ha")); |
||
return 0; |
return 0; |
||
}</lang> |
|||
A variation. |
|||
<lang c>... |
|||
char *string_repeat(char *str, int n) |
|||
{ |
|||
char *pa, *pb; |
|||
size_t slen = strlen(str); |
|||
char *dest = (char *)malloc(n*slen+1); |
|||
pa = dest + (n-1)*slen; |
|||
strcpy(pa, str); |
|||
pb = --pa + slen; |
|||
while (pa>=dest) *pa-- = *pb--; |
|||
return dest; |
|||
}</lang> |
}</lang> |
||