-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strjoin.c
More file actions
43 lines (40 loc) · 1.55 KB
/
ft_strjoin.c
File metadata and controls
43 lines (40 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tcharuel <tcharuel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 15:50:16 by tcharuel #+# #+# */
/* Updated: 2023/11/08 15:39:40 by tcharuel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief Allocates (with malloc(3)) and returns a new
* string, which is the result of the concatenation
* of ’s1’ and ’s2’.
*
* @param s1 The prefix string.
* @param s2 The suffix string.
*
* @return The newly allocated string.
* @retval NULL if the allocation fails.
*/
char *ft_strjoin(char const *s1, char const *s2)
{
char *buffer;
size_t s1_len;
size_t s2_len;
if (!s1 || !s2)
return (NULL);
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
buffer = (char *)malloc(
(s1_len + s2_len + 1) * sizeof(char));
if (!buffer)
return (NULL);
ft_strlcpy(buffer, s1, s1_len + 1);
ft_strlcat(buffer, s2, s1_len + s2_len + 1);
return (buffer);
}