Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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;
|
||||
}
|
||||
13
Task/Repeat-a-string/C/repeat-a-string-2.c
Normal file
13
Task/Repeat-a-string/C/repeat-a-string-2.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
...
|
||||
char *string_repeat(const char *str, int n)
|
||||
{
|
||||
char *pa, *pb;
|
||||
size_t slen = strlen(str);
|
||||
char *dest = malloc(n*slen+1);
|
||||
|
||||
pa = dest + (n-1)*slen;
|
||||
strcpy(pa, str);
|
||||
pb = --pa + slen;
|
||||
while (pa>=dest) *pa-- = *pb--;
|
||||
return dest;
|
||||
}
|
||||
17
Task/Repeat-a-string/C/repeat-a-string-3.c
Normal file
17
Task/Repeat-a-string/C/repeat-a-string-3.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char * char_repeat( int n, char c ) {
|
||||
char * dest = malloc(n+1);
|
||||
memset(dest, c, n);
|
||||
dest[n] = '\0';
|
||||
return dest;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char * result = char_repeat(5, '*');
|
||||
puts(result);
|
||||
free(result);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue