-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
48 lines (45 loc) · 1.61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: imunaev- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/06 23:36:33 by imunaev- #+# #+# */
/* Updated: 2025/01/18 12:11:17 by imunaev- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief Converts the initial portion of a string to an integer.
*
* This function skips leading whitespace, handles an optional '+' or '-' sign,
* and accumulates numeric characters until encountering a non-digit or the end
* of the string. The result is returned as an integer value.
*
* @param str The string to be converted.
* @return int The converted integer, or 0 if no valid conversion could be
* performed.
*/
int ft_atoi(const char *str)
{
int sign;
long long num;
num = 0;
sign = 1;
while (ft_isspace(*str))
str++;
if (*str == '-')
{
sign = -1;
str++;
}
else if (*str == '+')
str++;
while (ft_isdigit(*str))
{
num = num * 10 + (*str - '0');
str++;
}
return ((int)(num * sign));
}