-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathft_atoi.c
46 lines (43 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/13 14:41:25 by exam #+# #+# */
/* Updated: 2020/11/05 13:39:09 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: <stdlib.h>
** SYNOPSIS: convert ASCII string to integer
**
** DESCRIPTION:
** The atoi() function converts the initial portion of the string pointed
** to by str to int representation.
*/
int ft_atoi(const char *str)
{
int i;
int s;
int res;
i = 0;
s = 1;
res = 0;
while (str[i] == ' ' || str[i] == '\n' || str[i] == '\t' ||
str[i] == '\v' || str[i] == '\f' || str[i] == '\r')
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
s = -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
res = (res * 10) + (str[i] - '0');
i++;
}
return (res * s);
}