-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_strjoin.c
42 lines (38 loc) · 1.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cado-car <cado-car@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/31 11:45:46 by cado-car #+# #+# */
/* Updated: 2021/07/31 22:23:34 by cado-car ### ########lyon.fr */
/* */
/* ************************************************************************** */
/*
* DESCRIPTION
* Allocates (with malloc(3)) and returns a new string, which is the result of
* the concatenation of ’s1’ and ’s2’.
* PARAMETERS
* #1. The prefix string.
* #2. The suffix string.
* RETURN VALUES
* The new string. NULL if the allocation fails.
*/
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *ccat;
int len_s1;
int len_s2;
if (!s1 || !s2)
return (NULL);
len_s1 = (ft_strlen((char *)s1));
len_s2 = (ft_strlen((char *)s2));
ccat = (char *)malloc((len_s1 + len_s2 + 1) * sizeof(char));
if (ccat == NULL)
return (NULL);
ft_strlcpy(&ccat[0], s1, len_s1 + 1);
ft_strlcpy(&ccat[len_s1], s2, len_s2 + 1);
return (ccat);
}