-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
127 lines (116 loc) · 2.25 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
121
122
123
124
125
126
127
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mel-aini <mel-aini@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 12:16:41 by mel-aini #+# #+# */
/* Updated: 2022/11/21 12:18:01 by mel-aini ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t length(const char *s)
{
size_t i;
if (!s)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
int check_line(const char *s, int c)
{
size_t i;
if (!s)
return (0);
i = 0;
while (s[i])
{
if (s[i] == c)
return (1);
i++;
}
return (0);
}
char *join(char *s1, char *s2)
{
char *str;
size_t i;
size_t j;
if (!s1 && !s2)
return (NULL);
str = (char *)malloc(sizeof(char) * (length(s1) + length(s2) + 1));
if (!str)
return (NULL);
i = 0;
if (s1)
{
while (s1[i])
{
str[i] = s1[i];
i++;
}
}
j = -1;
while (s2[++j])
str[i + j] = s2[j];
str[i + j] = '\0';
if (s1)
free(s1);
return (str);
}
char *save(char *s, char c)
{
char *ret;
int i;
int len;
if (!*s)
return (NULL);
len = 0;
i = -1;
while (s[++i])
{
if (s[i] == c)
{
len = 1;
break ;
}
}
len = len + i;
ret = (char *)malloc(sizeof(char) * (len + 1));
if (!ret)
return (NULL);
i = -1;
while (++i < len)
ret[i] = s[i];
ret[i] = '\0';
return (ret);
}
char *remain(char *s, char c)
{
char *rem;
int i;
int j;
int len;
if (!s)
return (NULL);
i = 0;
if (!check_line(s, c))
return (NULL);
while (s[i] != c)
i++;
len = length(s) - i - 1;
rem = (char *)malloc(sizeof(char) * (len + 1));
if (!rem)
return (NULL);
j = 0;
while (j < len)
{
rem[j] = s[++i];
j++;
}
rem[j] = '\0';
return (rem);
}