-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
50 lines (46 loc) · 1.6 KB
/
ft_atoi.c
File metadata and controls
50 lines (46 loc) · 1.6 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ccarnot <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/05 18:27:40 by ccarnot #+# #+# */
/* Updated: 2023/05/05 18:56:01 by ccarnot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*******************************************
* Function: atoi
* Library: <stdlib.h>
* Description: converts the initial portion of the string pointed to by nptr to int
* Memory allocations: None
* Crash values:
* - nptr is NULL
* Return values:
* - the converted integer value
* - 0 if the input string cannot be converted
*******************************************/
int ft_atoi(const char *nptr)
{
int i;
int sign;
int n;
n = 0;
sign = 1;
i = 0;
while ((nptr[i] > 8 && nptr[i] < 14) || nptr[i] == 32)
i++;
if (nptr[i] == 43 || nptr[i] == 45)
{
if (nptr[i] == 45)
sign *= (-1);
i++;
}
while (nptr[i] > 47 && nptr[i] < 58)
{
n = n * 10 + nptr[i] - '0';
i++;
}
return (n * sign);
}