-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strjoin.c
57 lines (52 loc) · 1.67 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rakama <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/12 16:43:22 by rakama #+# #+# */
/* Updated: 2022/04/26 23:54:04 by rakama ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_return_result(char *result, const char *s1, const char *s2)
{
size_t len1;
size_t len2;
len1 = ft_strlen(s1);
len2 = ft_strlen(s2);
if (result == NULL)
return (NULL);
if (s1 == NULL)
{
ft_memcpy(result, s2, len2 + 1);
return (result);
}
else if (s2 == NULL)
{
ft_memcpy(result, s1, len1 + 1);
return (result);
}
if (s1 == NULL && s2 == NULL)
{
*result = '\0';
return (result);
}
return (result);
}
char *ft_strjoin(char const *s1, char const *s2)
{
size_t len1;
size_t len2;
char *result;
len1 = ft_strlen(s1);
len2 = ft_strlen(s2);
result = malloc(sizeof(char) * (len1 + len2) + 1);
if (result == NULL)
return (NULL);
ft_return_result(result, s1, s2);
ft_memcpy(result, s1, len1);
ft_memcpy(result + len1, s2, len2 + 1);
return (result);
}