-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strjoin.c
45 lines (42 loc) · 1.79 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: imunaev- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/07 12:57:30 by imunaev- #+# #+# */
/* Updated: 2025/01/18 12:39:32 by imunaev- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief Concatenates two strings into a newly allocated string.
*
* This function allocates memory and returns a new string that is the result
* of concatenating the strings `s1` and `s2`. The memory for the new string
* is obtained using `malloc` and must be freed by the caller.
*
* @param s1 The first string to concatenate.
* @param s2 The second string to concatenate.
* @return char* A pointer to the newly allocated string containing
* the concatenated result of `s1` and `s2`, or NULL if memory
* allocation fails.
*/
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t s1_len;
size_t s2_len;
if (!s1 || !s2)
return (NULL);
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
str = malloc(s1_len + s2_len + 1);
if (!str)
return (NULL);
ft_memcpy(str, s1, s1_len);
ft_memcpy(str + s1_len, s2, s2_len);
str[s1_len + s2_len] = '\0';
return (str);
}