-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strjoin.c
54 lines (49 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgasc <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/06 16:53:31 by lgasc #+# #+# */
/* Updated: 2023/10/26 19:11:31 by lgasc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
///Allocates (with malloc(3)) and returns a new string, which
///is the result of the concatenation of `string1` and `string2`.
///@param[in] string1 The prefix string.
///@param[in] string2 The suffix string.
///@returns The new string. NULL if the allocation fails.
///@remarks External function: `malloc`
char *ft_strjoin(char const *string1, char const *string2)
{
char *join;
unsigned int i;
unsigned int j;
if ((! string1) || (! string2))
return (NULL);
join = ft_calloc(
ft_strlen(string1) + ft_strlen(string2) + 1, sizeof * join);
if (join == NULL)
return (NULL);
i = 0;
while (string1[i])
{
join[i] = string1[i];
i++;
}
j = 0;
while (string2[j])
{
join[i + j] = string2[j];
j++;
}
join[i + j] = '\0';
return (join);
}
////////
char *ft_concatenate(const char *const start, const char *const end)
{
return (ft_strjoin(start, end));
}