-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strjoin.c
62 lines (58 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
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jamendoe <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/12 18:25:04 by jamendoe #+# #+# */
/* Updated: 2022/11/12 18:25:06 by jamendoe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t strleni(char const *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strjoin(char const *s1, char const *s2)
{
size_t i;
size_t j;
char *sj;
if (!s1 || !s2)
return (NULL);
sj = malloc(sizeof(char) * (strleni(s1) + strleni(s2) + 1));
if (!sj)
return (NULL);
i = 0;
while (s1[i] != '\0')
{
sj[i] = s1[i];
i++;
}
j = 0;
while (s2[j] != '\0')
{
sj[i + j] = s2[j];
j++;
}
sj[i + j] = '\0';
return (sj);
}
/*
Function name ft_strjoin
Prototype char *ft_strjoin(char const *s1, char const *s2);
Turn in files -
Parameters s1: The prefix string.
s2: The suffix string.
Return value The new string.
NULL if the allocation fails.
External functs. malloc
Description Allocates (with malloc(3)) and returns a new
string, which is the result of the concatenation
of ’s1’ and ’s2’.
*/