-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
108 lines (98 loc) · 2.15 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: isousa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/01 17:09:31 by isousa #+# #+# */
/* Updated: 2021/03/01 17:14:24 by isousa ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *ft_substr(char *s, int start, int len)
{
char *sub;
int i;
int s_len;
if (!s)
return (0);
s_len = ft_strlen(s);
if (start >= s_len)
{
sub = malloc(sizeof(char));
if (!sub)
return (0);
*sub = '\0';
return (sub);
}
if (s_len < len)
return (ft_strdup((char *)s + start));
i = 0;
sub = (char *)malloc(len + 1 * sizeof(char));
if (!sub)
return (0);
while (start < s_len && i < len)
sub[i++] = s[start++];
sub[i] = '\0';
return (sub);
}
char *ft_strdup(char *src)
{
char *dest;
int i;
if ((dest = malloc(ft_strlen(src) * sizeof(char) + 1)) == NULL)
return (0);
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
int ft_strchr(char *s, char c)
{
while (c != *s)
{
if (*s == 0)
return (0);
s++;
}
return (1);
}
char *ft_strjoin(char *s1, char *s2)
{
char *new;
int i;
int j;
if (!s1 || !s2)
return (0);
new = (char *)malloc(ft_strlen(s1) + ft_strlen(s2) + 1 * sizeof(char));
if (!new)
return (0);
i = 0;
j = 0;
while (s1[i] != '\0')
{
new[i] = s1[i];
i++;
}
while (s2[j] != '\0')
{
new[i] = s2[j];
j++;
i++;
}
new[i] = '\0';
return (new);
}