-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strjoin.c
executable file
·46 lines (39 loc) · 1.51 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: jchoy-me <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/11 14:29:45 by jchoy-me #+# #+# */
/* Updated: 2023/07/13 15:45:04 by jchoy-me ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
DESCRIPTION:
Allocates (with malloc(3)) and returns a new string, which is the result of
the concatenation of ’s1’ and ’s2’.
PARAMETERS:
s1: The prefix string.
s2: The suffix string.
RETURN VALUE:
The new string.
NULL if the allocation fails.
EXTERNAL FUNCTIONS:
malloc
*/
char *ft_strjoin(char const *s1, char const *s2)
{
char *dest;
size_t nbytes;
if (s1 == NULL || s2 == NULL)
return (NULL);
nbytes = ft_strlen(s1) + ft_strlen(s2) + 1;
dest = (char *) malloc(sizeof(char) * nbytes);
if (dest == NULL)
return (NULL);
ft_strlcpy(dest, s1, ft_strlen(s1) + 1);
ft_strlcat(dest, s2, nbytes);
return (dest);
}