-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_string.c
More file actions
97 lines (87 loc) · 2.03 KB
/
Copy pathutils_string.c
File metadata and controls
97 lines (87 loc) · 2.03 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpedraza <mpedraza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/25 01:30:19 by mpedraza #+# #+# */
/* Updated: 2026/01/22 21:47:20 by mpedraza ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
if (n == 0)
return (0);
i = 0;
while (*s1 == *s2 && *s1 && i + 1 < n)
{
s1++;
s2++;
i++;
}
return ((unsigned char)*s1 - (unsigned char)*s2);
}
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s && s[i])
i++;
return (i);
}
char *ft_strchr(const char *s, int c)
{
if (!s)
return (NULL);
while (*s)
{
if (*s == (unsigned char)c)
return ((char *)s);
s++;
}
if ((unsigned char)c == '\0')
return ((char *)s);
return (NULL);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *dest;
size_t len1;
size_t len2;
size_t i;
len1 = 0;
len2 = 0;
if (s1)
len1 = ft_strlen(s1);
if (s2)
len2 = ft_strlen(s2);
dest = malloc((len1 + len2 + 1));
if (!dest)
return (NULL);
i = 0;
while (i < len1)
dest[i++] = *(s1++);
i = 0;
while (i < len2)
dest[len1 + i++] = *(s2++);
dest[len1 + len2] = '\0';
return (dest);
}
char *ft_strdup(const char *s)
{
char *dup;
size_t slen;
int i;
slen = ft_strlen(s);
dup = malloc(slen + 1);
if (!dup)
return (NULL);
i = 0;
while (*s)
dup[i++] = *s++;
dup[i] = '\0';
return (dup);
}