-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strjoin.c
60 lines (55 loc) · 1.53 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gudaniel <gudaniel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:59:09 by gudaniel #+# #+# */
/* Updated: 2024/04/09 13:59:18 by gudaniel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t count_len(const char *str)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *res;
int i;
int k;
i = 0;
k = 0;
if (!s1 || !s2)
return (NULL);
res = (char *)malloc(count_len(s1) + count_len(s2) + 1);
if (res == NULL)
return (NULL);
while (s1[i])
{
res[i] = s1[i];
i++;
}
while (s2[k])
{
res[i + k] = s2[k];
k++;
}
res[i + k] = '\0';
return (res);
}
/* int main(void)
{
char *s1 = "my favorite animal is";
char *s2 = "|||";
char *s3 = "the nyancat";
char *res = ft_strjoin(ft_strjoin(s1, s2), s3);
printf("%s\n", res);
} */