-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
120 lines (109 loc) · 2.39 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
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jcampagn <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 12:01:58 by jcampagn #+# #+# */
/* Updated: 2021/12/05 15:21:52 by jcampagn ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
if (!s)
return (0);
while (s && s[i])
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_get_line(char *left_str)
{
int i;
char *str;
i = 0;
if (!left_str[i])
return (NULL);
while (left_str[i] && left_str[i] != '\n')
i++;
str = (char *)malloc(sizeof(char) * (i + 2));
if (!str)
return (NULL);
i = 0;
while (left_str[i] && left_str[i] != '\n')
{
str[i] = left_str[i];
i++;
}
if (left_str[i] == '\n')
{
str[i] = left_str[i];
i++;
}
str[i] = '\0';
return (str);
}
char *ft_new_left_str(char *left_str)
{
int i;
int j;
char *str;
i = 0;
while (left_str[i] && left_str[i] != '\n')
i++;
if (!left_str[i])
{
free(left_str);
return (NULL);
}
str = (char *)malloc(sizeof(char) * (ft_strlen(left_str) - i + 1));
if (!str)
return (NULL);
i++;
j = 0;
while (left_str[i])
str[j++] = left_str[i++];
str[j] = '\0';
free(left_str);
return (str);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
int i;
size_t len;
str = 0;
str = malloc(ft_strlen((char *)s1) + ft_strlen((char *)s2) + 1);
if (!str)
return (NULL);
len = 0;
while (s1 && s1[len])
{
str[len] = s1[len];
len++;
}
i = -1;
while ((char *)s2 && s2[++i])
str[len++] = s2[i];
str[len] = '\0';
return (str);
}