-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
90 lines (81 loc) · 1.27 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
#include "header/pipex_bonus.h"
size_t ft_strlen_gnl(const char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strchr_gnl(const char *string, int symbol)
{
int i;
unsigned char *result;
i = 0;
while ((unsigned char)string[i] != (unsigned char)symbol)
{
if (string[i] == '\0')
return (0);
i++;
}
result = (unsigned char *) &string[i];
return ((char *)(result));
}
char *ft_strjoin_gnl(char *s1, char *s2)
{
int k;
int n;
char *str;
if (!s1)
return (NULL);
k = 0;
n = 0;
str = malloc(sizeof(char) * (ft_strlen_gnl(s1) + ft_strlen_gnl(s2) + 1));
if (!str)
return (NULL);
while (s1[k] != '\0')
{
str[k] = s1[k];
k++;
}
while (s2[n])
{
str[k] = s2[n];
k++;
n++;
}
str[k] = '\0';
return (str);
}
static size_t ft_min_gnl(size_t a, size_t b)
{
if (a < b)
return (a);
else
return (b);
}
char *ft_substr_gnl(char const *s, unsigned int start, size_t len)
{
size_t i;
char *s2;
size_t lenght;
if (!s)
return (NULL);
i = 0;
lenght = ft_strlen_gnl(s);
s2 = malloc(sizeof(char) * (ft_min_gnl(len, lenght) + 1));
if (!s2)
return (NULL);
s2[i] = '\0';
if (start >= lenght)
return (s2);
while (i < len && s[start + i] != '\0')
{
s2[i] = s[start + i];
i++;
}
s2[i - 1] = '\0';
return (s2);
}