Repository navigation
Expand file tree
/
Copy pathft_strjoin.c
More file actions
69 lines (62 loc) · 1.73 KB
/
Copy pathft_strjoin.c
File metadata and controls
69 lines (62 loc) · 1.73 KB
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
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asalniko <asalniko@student.42berlin.d +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 16:30:02 by asalniko #+# #+# */
/* Updated: 2025/04/07 20:18:57 by asalniko ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
if (!str)
return (0);
while (str[i] != '\0')
i++;
return (i);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *newstr;
size_t i;
size_t j;
i = 0;
j = 0;
if (!s1 || !s2)
return (NULL);
newstr = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (!newstr)
return (NULL);
while (s1[i])
{
newstr[i] = s1[i];
i++;
}
while (s2[j])
newstr[i++] = s2[j++];
newstr[i] = '\0';
return (newstr);
}
/*
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *prefix = "abc";
char *suffix = "def";
char *result;
result = ft_strjoin(prefix, suffix);
if (!result)
{
printf("memory allocation failed\n");
return (1);
}
printf("prefix \"%s\" + suffix \"%s\" = \"%s\"\n", prefix, suffix, result);
free(result);
return (0);
}*/