-
Notifications
You must be signed in to change notification settings - Fork 0
/
libft_functions_2.c
85 lines (77 loc) · 1.92 KB
/
libft_functions_2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft_functions_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asfaihi <asfaihi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/08 16:53:52 by asfaihi #+# #+# */
/* Updated: 2020/11/03 12:36:14 by asfaihi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
if (s == NULL)
return (0);
while (*s != '\0')
{
i++;
s++;
}
return (i);
}
int ft_atoi(const char *str)
{
long a;
int b;
int c;
a = 0;
b = 0;
c = 1;
while (((*str <= 13) && (*str >= 9)) || (*str == 32))
str++;
while ((*str == '+') || (*str == '-'))
{
if (*str == '-')
c = -1;
b++;
str++;
}
if (b > 1)
return (0);
while ((*str <= '9') && (*str >= '0'))
{
a = (*str - 48) + a * 10;
str++;
}
return (a * c);
}
static size_t ft_smallest(const char *s, unsigned int start, size_t len)
{
if (len > ft_strlen(s + start))
return (ft_strlen(s + start) + 1);
return (len + 1);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *x;
size_t i;
if (s == NULL)
return (NULL);
if (start > ft_strlen(s))
len = 0;
i = 0;
x = (char *)malloc(sizeof(char) * ft_smallest(s, start, len));
if (x == 0)
return (NULL);
while (i < (ft_smallest(s, start, len) - 1))
{
x[i] = s[i + start];
i++;
}
x[i] = '\0';
return (x);
}