-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
114 lines (103 loc) · 2.41 KB
/
get_next_line_utils_bonus.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: codespace <codespace@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 09:40:12 by xjose #+# #+# */
/* Updated: 2024/07/25 18:14:06 by codespace ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
if (!s)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strchr(char *s, int c)
{
int i;
i = 0;
if (!s)
return (0);
if (c == '\0')
return ((char *)&s[ft_strlen(s)]);
while (s[i] != '\0')
{
if (s[i] == (char)c)
return ((char *)&s[i]);
i++;
}
return (0);
}
char *ft_strdup(char *s1)
{
char *str_dup;
size_t len;
len = ft_strlen(s1);
str_dup = (char *)malloc(sizeof(char) * (len + 1));
if (!str_dup)
return (NULL);
len = -1;
while (s1[++len])
str_dup[len] = s1[len];
str_dup[len] = '\0';
return (str_dup);
}
char *ft_strjoin(char *s1, char *s2)
{
char *join;
size_t idx;
size_t x;
if (!s1)
{
s1 = (char *)malloc(sizeof(char) * 1);
s1[0] = '\0';
}
if (!s1 || !s2)
return (NULL);
join = malloc(sizeof(char) * ((ft_strlen(s1) + ft_strlen(s2)) + 1));
if (join == NULL)
return (NULL);
idx = -1;
x = 0;
if (s1)
while (s1[++idx] != '\0')
join[idx] = s1[idx];
while (s2[x] != '\0')
join[idx++] = s2[x++];
join[ft_strlen(s1) + ft_strlen(s2)] = '\0';
free(s1);
return (join);
}
char *ft_substr(char *s, unsigned int start, size_t len)
{
char *substr;
size_t tmp_len;
size_t i;
if (!s || !len)
return (NULL);
tmp_len = ft_strlen(s);
if (start >= tmp_len)
len = 0;
if (len > tmp_len - start)
len = tmp_len - start;
substr = (char *)malloc(len + 1);
if (!substr)
return (NULL);
i = 0;
while (i < len && s[start])
{
substr[i] = s[start];
++start;
++i;
}
substr[i] = '\0';
return (substr);
}