-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
102 lines (92 loc) · 2.28 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rde-lima <rde-lima@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:02:04 by rde-lima #+# #+# */
/* Updated: 2021/10/18 15:11:29 by rde-lima ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *str)
{
size_t len;
len = 0;
if (!str)
return (len);
while (str[len] != '\0')
++len;
return (len);
}
char *ft_strchr(const char *s, int c)
{
if (!s)
return (NULL);
while (*s)
{
if (*s == (char)c)
return ((char *)s);
s++;
}
if (c == 0)
return ((char *)s);
return (NULL);
}
char *ft_strdup(const char *str1)
{
size_t counter;
char *str;
if (!str1)
return (NULL);
str = malloc(ft_strlen(str1) + 1);
if (!str)
return (NULL);
counter = 0;
while (*str1 != '\0')
str[counter++] = *str1++;
str[counter] = '\0';
return (str);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t size;
size = 0;
if (!s1 && !s2)
return (NULL);
if (!s1)
return (ft_strdup(s2));
if (!s2)
return (ft_strdup(s1));
str = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
while (*s1 != '\0')
str[size++] = *s1++;
while (*s2 != '\0')
str[size++] = *s2++;
str[size] = '\0';
return (str);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t size;
char *str;
if (!s)
return (NULL);
if (start > ft_strlen(s))
return (ft_strdup(""));
if (start + len > ft_strlen(s))
len = ft_strlen(s) - start;
str = (char *)malloc(sizeof(char) * len + 1);
if (!str)
return (NULL);
size = 0;
while (s[start + size] && size < len)
{
str[size] = s[start + size];
++size;
}
str[size] = '\0';
return (str);
}