-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
115 lines (104 loc) · 2.4 KB
/
get_next_line_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: arsobrei <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/09 10:06:17 by arsobrei #+# #+# */
/* Updated: 2023/08/31 12:00:32 by arsobrei ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t length;
length = 0;
while (s[length] != '\0')
{
length++;
}
return (length);
}
char *ft_strncpy(char *dest, const char *src, size_t n)
{
size_t index;
index = 0;
while ((src[index] != '\0') && (index < n))
{
dest[index] = src[index];
index++;
}
while (index < n)
{
dest[index] = '\0';
index++;
}
return (dest);
}
char *ft_strchr(const char *s, int c)
{
size_t index;
char *first_occ;
if (s == NULL)
return (NULL);
index = 0;
first_occ = NULL;
while (s[index] != '\0')
{
if (s[index] == (unsigned char)c)
{
first_occ = (char *)&s[index];
return (first_occ);
}
index++;
}
if ((unsigned char)c == '\0')
{
first_occ = (char *)&s[index];
}
return (first_occ);
}
char *ft_strdup(const char *s)
{
size_t index;
char *new_string;
new_string = malloc(ft_strlen(s) + 1);
index = 0;
if (new_string == NULL)
{
return (NULL);
}
while (s[index])
{
new_string[index] = s[index];
index++;
}
new_string[index] = '\0';
return (new_string);
}
char *ft_strjoin(char *s1, char *s2)
{
int result_index;
int index;
char *result;
if (s1 == NULL)
s1 = ft_strdup("");
result = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (result == NULL)
return (NULL);
result_index = 0;
index = 0;
while (s1[index])
{
result[result_index++] = s1[index++];
}
index = 0;
while (s2[index])
{
result[result_index++] = s2[index++];
}
result[result_index] = '\0';
free((char *)s1);
return (result);
}