This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
executable file
·52 lines (48 loc) · 1.58 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
51
52
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_atoi.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/08/17 15:17:14 by fbes #+# #+# */
/* Updated: 2022/02/08 19:48:05 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_is_odd_or_even(int n)
{
if (n == 0 || n % 2 == 0)
return (1);
else
return (-1);
}
/**
* Parse a number in a string to an integer
* @param[in] *str The string to parse
* @return The parsed number
*/
int ft_atoi(const char *str)
{
int num;
int i;
int signs;
num = 0;
signs = 0;
i = 0;
while (str[i] != '\0')
{
if (ft_iswhitespace(str[i]) && num == 0 && signs == 0)
num = 0;
else if (str[i] >= '0' && str[i] <= '9')
num = num * 10 + ((int)str[i] - 48);
else if (str[i] == '-' && signs == 0 && num == 0)
signs += 1;
else if (str[i] == '+' && signs == 0 && num == 0)
signs += 2;
else
break ;
i++;
}
return (num * ft_is_odd_or_even(signs));
}