blob: bc00216656473eb2651308b55d1fd26a1eee4fc1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/// Prints *str* *times* to standard output.
int
repeat(const int times, const char* str)
{
for (int i = 0; i < times; ++i)
printf("%s", str);
return printf("\n");
}
/// Concatenates strings from *strv*. User needs to free allocated string.
char*
concat(const int strc, const char* strv[])
{
assert(0 < strc);
int i;
int len = 0;
for (i = 0; i < strc; ++i)
len += 1 + strlen(strv[i]);
char* const str = malloc(len);
str[0] = 0;
char* ptr = stpcpy(str, strv[0]);
for (int i = 1; i < strc; ++i) {
*ptr = ' ';
*++ptr = 0;
ptr = stpcpy(ptr, strv[i]);
}
return str;
}
int
main(const int argc, const char* argv[])
{
if (3 > argc)
return 1;
const int times = atoi(argv[1]);
if (1 > times)
return 1;
const char* str = concat(argc - 2, &argv[2]);
return repeat(times, str);
}
|