-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_implode_strings.c
More file actions
62 lines (57 loc) · 1.7 KB
/
ft_implode_strings.c
File metadata and controls
62 lines (57 loc) · 1.7 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_implode_strings.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sclolus <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/05 10:53:13 by sclolus #+# #+# */
/* Updated: 2017/04/05 11:32:24 by sclolus ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static uint32_t *ft_get_lens(char **strings, uint32_t *count, uint32_t *len)
{
uint32_t i;
uint32_t *lens;
uint32_t tmp;
i = 0;
*count = 0;
while (strings[i++])
(*count)++;
if (!*count)
return (NULL);
if (!(lens = (uint32_t*)malloc(sizeof(uint32_t) * *count)))
exit(EXIT_FAILURE);
tmp = *count;
i = 0;
while (i < tmp)
{
lens[i] = ft_strlen(strings[i]);
*len += lens[i++];
}
return (lens);
}
char *ft_implode_strings(char **strings)
{
char *str;
uint32_t *lens;
uint32_t count;
uint32_t len;
uint32_t i;
i = 0;
len = 0;
if (!strings || !(lens = ft_get_lens(strings, &count, &len)))
return (NULL);
if (!(str = ft_strnew(len)))
exit(EXIT_FAILURE);
i = 0;
len = 0;
while (i < count)
{
ft_memcpy(str + len, strings[i], lens[i]);
len += lens[i++];
}
free(lens);
return (str);
}