-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strjoin.c
executable file
·85 lines (78 loc) · 1.99 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
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tseguier <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/25 16:17:22 by tseguier #+# #+# */
/* Updated: 2014/03/27 18:44:12 by jcoignet ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <stdarg.h>
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *newstr;
if (!s1 || !s2)
return (NULL);
newstr = ft_strnew(ft_strlen(s1) + ft_strlen(s2));
if (!newstr)
return (NULL);
ft_strcpy(newstr, s1);
return (ft_strcat(newstr, s2));
}
char *ft_strmjoin(int nbstr, ...)
{
char *newstr;
va_list list;
char *strings[nbstr];
int i;
size_t len;
i = 0;
len = 0;
va_start(list, nbstr);
while (i < nbstr)
{
strings[i] = va_arg(list, char *);
len += ft_strlen(strings[i]);
++i;
}
newstr = ft_strnew(len + 1);
i = 0;
while (i < nbstr)
{
ft_strcat(newstr, strings[i]);
++i;
}
va_end(list);
return (newstr);
}
char *ft_strmjoinclean(int nbstr, ...)
{
char *newstr;
va_list list;
char **strings[nbstr];
int i;
size_t totallen;
i = 0;
totallen = 0;
va_start(list, nbstr);
while (i < nbstr)
{
strings[i] = va_arg(list, char **);
totallen += ft_strlen(*strings[i]);
++i;
}
newstr = ft_strnew(totallen + 1);
i = 0;
while (i < nbstr)
{
ft_strcat(newstr, *strings[i]);
ft_strdel(strings[i]);
++i;
}
va_end(list);
return (newstr);
}