-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strlcat.c
64 lines (59 loc) · 2.24 KB
/
ft_strlcat.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
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jopedro3 <jopedro3@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/04 12:00:46 by jopedro3 #+# #+# */
/* Updated: 2023/10/04 14:45:59 by jopedro3 ### ########.fr */
/* */
/* ************************************************************************** */
/*
Function: ft_strlcat
Purpose: Concatenates two strings (dst and src) safely,
ensuring that the resulting string is null-terminated.
How it works:
1. Finds the lengths of dst and src using ft_strlen.
2. Checks if size (maximum length to concatenate)
is less than or equal to dst_len. If true, returns
the sum of size and src_len.
3. Initializes len to 0, which will be used to
iterate through src.
4. Iterates through src, copying characters to dst,
ensuring not to exceed the size limit.
5. Null-terminates the concatenated string in dst.
6. Returns the total length of the string that
would have been created if there were enough space.
*/
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t dst_len;
size_t src_len;
size_t len;
dst_len = ft_strlen(dst);
src_len = ft_strlen(src);
if (size <= dst_len)
return (size + src_len);
len = 0;
while (src[len] && (dst_len + len + 1) < size)
{
dst[dst_len + len] = src[len];
len++;
}
dst[dst_len + len] = '\0';
return (dst_len + src_len);
}
/*#include <stdio.h>
int main(void)
{
char dest[20] = "Hello";
char *src = " World!";
size_t size = 20;
size_t result;
result = ft_strlcat(dest, src, size);
printf("Result: %zu\n", result);
printf("Concatenated string: %s\n", dest);
return 0;
}*/