-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
62 lines (56 loc) · 1.57 KB
/
ft_atoi.c
File metadata and controls
62 lines (56 loc) · 1.57 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: omaly <omaly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/03 15:01:11 by omaly #+# #+# */
/* Updated: 2025/09/19 18:05:36 by omaly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'
|| c == ' ');
}
static int ft_getstartidx(const char *s)
{
int idx;
idx = 0;
while (s[idx] != 0)
{
if (!ft_isspace(s[idx]))
return (idx);
idx++;
}
return (-1);
}
int ft_atoi(const char *s)
{
int start_pos;
int pos;
int sign;
int acc;
start_pos = ft_getstartidx(s);
if (start_pos == -1)
return (0);
pos = start_pos;
sign = 0;
if (s[pos] == '-' || s[pos] == '+')
{
if (s[pos] == '-')
sign = 1;
pos++;
}
acc = 0;
while (s[pos] != 0 && ft_isdigit(s[pos]))
{
acc = acc * 10 + s[pos] - '0';
pos++;
}
if (sign)
return (acc * -1);
return (acc);
}