-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
74 lines (67 loc) · 1.97 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbin-nas <mbin-nas@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 14:19:55 by mbin-nas #+# #+# */
/* Updated: 2022/09/28 15:44:59 by mbin-nas ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
//A function to read the number of characters in the string
size_t ft_strlen(char *string)
{
size_t i;
if (!string)
return (0);
i = 0;
while (string[i] != '\0')
i++;
return (i);
}
//A function to find the \n or \0
char *ft_strchr(char *string, int search_char)
{
int i;
i = 0;
if (!string)
return (0);
if (search_char == '\0')
return ((char *)&string[ft_strlen(string)]);
while (string[i] != '\0')
{
if (string[i] == (char)search_char)
return ((char *)&string[i]);
i++;
}
return (0);
}
// A function that joins everthing to the end of tmp_buffer variable
char *ft_strjoin(char *s1, char *s2)
{
size_t i;
size_t j;
char *store_str;
if (!s1)
{
s1 = (char *)malloc(1 * sizeof(char));
s1[0] = '\0';
}
if (!s1 || !s2)
return (NULL);
store_str = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (store_str == NULL)
return (NULL);
i = -1;
j = 0;
if (s1)
while (s1[++i] != '\0')
store_str[i] = s1[i];
while (s2[j] != '\0')
store_str[i++] = s2[j++];
store_str[ft_strlen(s1) + ft_strlen(s2)] = '\0';
free(s1);
return (store_str);
}