-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strjoin.c
46 lines (42 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vinguyen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/11 00:00:19 by vinguyen #+# #+# */
/* Updated: 2019/10/11 00:00:21 by vinguyen ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Allocates using malloc and returns fresh string ending with '\0'
** Result of the concat of s1 and s2. If alloc fails, returns NULL
** Param: prefix string, suffix string
** Return: new string
*/
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
size_t i;
size_t j;
size_t clen;
char *s;
if (!s1 || !s2)
return (NULL);
clen = ft_strlen(s1) + ft_strlen(s2);
i = 0;
j = 0;
s = (char*)malloc(sizeof(char) * (clen + 1));
if (!s)
return (NULL);
while (i < ft_strlen(s1))
{
s[i] = s1[i];
i++;
}
while (i < clen)
s[i++] = s2[j++];
s[i] = '\0';
return (s);
}