-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strjoin.c
53 lines (50 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vcodrean <vcodrean@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/21 12:19:52 by vcodrean #+# #+# */
/* Updated: 2022/10/01 10:17:24 by vcodrean ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* It takes two strings, allocates a new string of the appropriate size,
* copies the first string into the new string, then copies the second
* string into the new string, and returns the new string
*
* param char s1 The first string to be joined.
* param char s2 The string to be appended to the end of s1.
*
* return A pointer to a new string that is the result of the concatenation
* of s1 and s2.
*/
char *ft_strjoin(char const *s1, char const *s2)
{
int i;
int len1;
int len2;
char *str;
if (s1 && s2)
{
len1 = ft_strlen(s1);
len2 = ft_strlen(s2);
str = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
if (str == NULL)
return (NULL);
i = -1;
while (s1[++i])
str[i] = s1[i];
i = -1;
while (s2[++i])
{
str[len1] = s2[i];
len1++;
}
str[len1] = '\0';
return (str);
}
return (NULL);
}