-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
49 lines (45 loc) · 1.53 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvenance <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/24 20:07:31 by vvenance #+# #+# */
/* Updated: 2015/12/15 17:58:21 by vvenance ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void if_signe(int *i, int *j, int *k, const char *str)
{
while (str[*i] == '-' || str[*i] == '+')
{
if (str[*i] == '+')
(*k)++;
if (str[*i] == '-')
(*j)++;
(*i)++;
}
}
int ft_atoi(const char *str)
{
int i;
int j;
int k;
int nbr;
i = 0;
j = 0;
k = 0;
nbr = 0;
while (str[i] == '\n' || str[i] == '\v' || str[i] == '\t' || str[i] == '\r'
|| str[i] == '\f' || str[i] == ' ')
i++;
if_signe(&i, &j, &k, str);
while (str[i] != '\0' && str[i] >= '0' && str[i] <= '9')
nbr = (str[i++] - '0') + (10 * nbr);
if (j == 1)
nbr = nbr * -1;
if ((j > 1 || k > 1) || (j == 1 && k == 1))
nbr = 0;
return (nbr);
}