-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_strsjoin.c
44 lines (39 loc) · 1.39 KB
/
ft_strsjoin.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gamarcha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/04 22:26:30 by gamarcha #+# #+# */
/* Updated: 2021/06/03 13:23:49 by gamarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t count_str(const char **strs, const char *sep)
{
size_t count;
size_t i;
count = 0;
i = 0;
while (strs[i])
count += ft_strlen(strs[i++]);
return (count + ft_strlen(sep) * (i - 1));
}
char *ft_strsjoin(const char **strs, const char *sep)
{
char *str;
size_t i;
str = (char *)malloc(sizeof(char) * (count_str(strs, sep) + 1));
if (str == 0)
return (0);
*str = 0;
i = 0;
while (strs[i])
{
if (i)
ft_strcat(str, sep);
ft_strcat(str, strs[i++]);
}
return (str);
}