-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
108 lines (97 loc) · 2.08 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: mbouderr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 06:18:18 by mbouderr #+# #+# */
/* Updated: 2023/01/12 16:48:43 by mbouderr ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
void *ft_calloc(size_t count, size_t size)
{
char *ptr;
size_t i;
i = 0;
ptr = malloc(size * count);
if (!ptr)
return (NULL);
while (i < (count * size))
{
ptr[i] = '\0';
i++;
}
return (ptr);
}
size_t ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strjoin(char *s1, char *s2)
{
size_t i;
char *p;
size_t len_s1;
size_t len_s2;
len_s1 = ft_strlen(s1);
len_s2 = ft_strlen(s2);
i = 0;
p = (char *)ft_calloc(((len_s1 + len_s2) + 1), sizeof(char));
if (p == NULL)
return (NULL);
while (i < len_s1)
{
p[i] = s1[i];
i++;
}
i = 0;
while (i <= len_s2)
{
p[i + len_s1] = s2[i];
i++;
}
free(s1);
return (p);
}
ssize_t ft_strchr(char *s, int c)
// if 'c is available return (i) else return (-1)
{
int len;
int i;
len = ft_strlen(s);
i = 0;
while (i <= len)
{
if (s[i] == c)
{
return (i);
}
i++;
}
return (-1);
}
char *ft_substr(char *s, unsigned int start, size_t len)
{
size_t i;
size_t j;
char *str;
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
i = start;
j = 0;
while (s[i] && i < len + start)
{
str[j++] = s[i++];
}
str[j] = '\0';
return (str);
}