-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
50 lines (47 loc) · 1.9 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmaurer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/24 17:08:33 by mmaurer #+# #+# */
/* Updated: 2021/09/07 21:31:25 by mmaurer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* The atoi() function converts the initial portion of the string pointed to by
* nptr to int.
* Gracefull handles non-printable characters at the beginning of the string,
* then flips sign if required and then converts character to digit until the
* end of the string is reached using this algorithm result * 10 + (str[i] - '0')
* and stores in result.
1. We start by initializing i to 0 and result to 0.
2. We then loop through the string until we reach the end of the string or a
non-digit character.
3. If we encounter a ‘-‘, we set sign to -1.
4. If we encounter a ‘+’, we set sign to 1.
*/
int ft_atoi(const char *str)
{
size_t i;
size_t result;
int sign;
i = 0;
result = 0;
sign = 1;
while ((str[i] == '\n' || str[i] == '\t' || str[i] == ' ')
|| str[i] == '\v' || str[i] == '\f' || str[i] == '\r')
i++;
if (str[i] == '-')
sign = -1;
if (str[i] == '-' || str[i] == '+')
i++;
while (str[i] >= '0' && str[i] <= '9')
{
result = result * 10 + (str[i] - '0');
i++;
}
return (result * sign);
}