This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathft_atoi.c
58 lines (53 loc) · 1.51 KB
/
ft_atoi.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dground <dground@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/07 12:42:18 by dground #+# #+# */
/* Updated: 2021/10/11 21:49:45 by dground ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char str)
{
if ((str == ' ') || (str == '\t') || (str == '\r'))
return (1);
else if ((str == '\v') || (str == '\n') || (str == '\f'))
return (1);
else
return (0);
}
typedef struct s_var
{
int k;
int result;
} t_var;
int ft_atoi(const char *str)
{
t_var t;
t.result = 0;
t.k = 0;
while (ft_isspace(*str))
str++;
if (*str == '+')
{
str++;
if (*str == '-')
return (t.result);
}
if (*str == '-')
{
t.k = 1;
str++;
}
while (*str >= '0' && *str <= '9')
{
t.result = t.result * 10 + (int)*str - '0';
str++;
}
if (t.k == 1)
return (t.result * (-1));
return (t.result);
}