-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_next_line_utils_bonus.c
executable file
·92 lines (82 loc) · 2.18 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jwon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/09 16:34:18 by jwon #+# #+# */
/* Updated: 2020/04/17 16:10:07 by jwon ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *str)
{
int idx;
idx = 0;
while (str[idx])
idx++;
return (idx);
}
char *ft_strdup(const char *str)
{
int idx;
char *dest;
if (!(dest = (char *)malloc(sizeof(char) * (ft_strlen(str) + 1))))
return (NULL);
idx = 0;
while (str[idx])
{
dest[idx] = str[idx];
idx++;
}
dest[idx] = '\0';
return (dest);
}
char *ft_strchr(const char *str, int c)
{
while (*str)
{
if (*str == c)
return ((char *)str);
str++;
}
if (c == '\0')
return ((char *)str);
return (0);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
unsigned int idx;
size_t s_len;
char *modified_s;
if (!s || !(modified_s = malloc(sizeof(char) * (len + 1))))
return (NULL);
s_len = ft_strlen((char *)s);
idx = 0;
while (start < s_len && s[start + idx] && idx < len)
{
modified_s[idx] = s[start + idx];
idx++;
}
modified_s[idx] = '\0';
return (modified_s);
}
char *ft_strjoin(char *s1, char *s2)
{
int idx;
int idx_join;
char *join;
if (!s1 || !s2 || !(join = malloc(sizeof(char) *
(ft_strlen((char *)s1) + ft_strlen((char *)s2) + 1))))
return (NULL);
idx = 0;
idx_join = 0;
while (s1[idx])
join[idx_join++] = s1[idx++];
idx = 0;
while (s2[idx])
join[idx_join++] = s2[idx++];
join[idx_join] = '\0';
return (join);
}