-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
62 lines (58 loc) · 1.2 KB
/
ft_atoi.c
File metadata and controls
62 lines (58 loc) · 1.2 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
int main_func(const char *str, int level, int pos, int result)
{
int counter;
int cur;
counter = 0;
if (str[counter] == '-')
{
pos = 0;
counter++;
}
while (str[counter] != '\0' &&
str[counter] >= '0' && str[counter] <= '9')
counter++;
counter--;
while (counter != -2)
{
if (str[counter] < '0' || str[counter] > '9')
break ;
cur = (str[counter] - 48) * level;
if (cur < result && cur != 0)
return (pos ? -1 : 0);
result += cur;
level *= 10;
counter--;
}
return (pos ? result : result * -1);
}
int middleware(const char *str, int level, int pos, int result)
{
while (*str < '0' || *str > '9')
{
if (*str != ' ' && *str != '\t' && *str != '\n' &&
*str != '\v' && *str != '\f' && *str != '\r' &&
*str != '-' && *str != '+')
return (0);
if (*str == '-')
pos = 0;
if (*str == '-' && (str[1] < '0' || str[1] > '9'))
return (0);
if ((*str == '-' || *str == '+') &&
(str[1] == '-' || str[1] == '+'))
return (0);
str++;
}
result = main_func(str, level, pos, result);
return (result);
}
int ft_atoi(const char *str)
{
int result;
int level;
int pos;
result = 0;
level = 1;
pos = 1;
result = middleware(str, level, pos, result);
return (result);
}