tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
22
Task/Repeat-a-string/C/repeat-a-string-1.c
Normal file
22
Task/Repeat-a-string/C/repeat-a-string-1.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char * string_repeat( int n, const char * s ) {
|
||||
size_t slen = strlen(s);
|
||||
char * dest = malloc(n*slen+1);
|
||||
|
||||
int i; char * p;
|
||||
for ( i=0, p = dest; i < n; ++i, p += slen ) {
|
||||
memcpy(p, s, slen);
|
||||
}
|
||||
*p = '\0';
|
||||
return dest;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char * result = string_repeat(5, "ha")
|
||||
puts(result);
|
||||
free(result);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue