-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strjoin.c
75 lines (68 loc) · 1.87 KB
/
ft_strjoin.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rvandepu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/19 12:12:11 by rvandepu #+# #+# */
/* Updated: 2024/10/13 04:02:37 by rvandepu ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdbool.h>
#include <stdlib.h>
#include "libft.h"
char *ft_strjoin(int count, char **strs)
{
char *s;
int len;
int i;
int j;
len = 0;
i = 0;
while ((count < 0 || i < count) && strs[i])
len += ft_strlen(strs[i++]);
count = i;
s = malloc(len + 1);
if (s == NULL)
return (s);
s[len] = '\0';
len = 0;
i = 0;
while (i < count)
{
j = 0;
while (strs[i][j])
s[len++] = strs[i][j++];
i++;
}
return (s);
}
static bool joinv(int count, char **ret, va_list *arg, size_t len)
{
char *s;
if (count != 0)
s = va_arg(*arg, char *);
if (count == 0 || s == NULL)
{
*ret = malloc(len + 1);
if (*ret == NULL)
return (false);
(*ret)[len] = '\0';
return (true);
}
if (!joinv((count * (count > 0)) - 1, ret, arg, len + ft_strlen(s)))
return (false);
while (*s)
(*ret)[len++] = *s++;
return (true);
}
char *ft_strjoinv(int count, ...)
{
char *s;
va_list arg;
va_start(arg, count);
if (!joinv(count, &s, &arg, 0))
return (va_end(arg), NULL);
return (va_end(arg), s);
}