mirror of
https://github.com/guezoloic/libft.git
synced 2026-01-25 04:34:14 +00:00
fix(ft_atoi): rework function
- handle minus sign - allow space before number
This commit is contained in:
26
ft_atoi.c
26
ft_atoi.c
@@ -3,21 +3,27 @@
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int value;
|
||||
size_t len;
|
||||
size_t i;
|
||||
int n;
|
||||
int is_negative;
|
||||
|
||||
value = 0;
|
||||
len = ft_strlen(str);
|
||||
i = 0;
|
||||
while (i < len)
|
||||
is_negative = 0;
|
||||
while (str[i] == ' ')
|
||||
i++;
|
||||
if (str[i] == '-')
|
||||
{
|
||||
if (!ft_isdigit(str[i]))
|
||||
return (value);
|
||||
n = (int)(str[i] - '0');
|
||||
value *= 10;
|
||||
value += n;
|
||||
is_negative = 1;
|
||||
i++;
|
||||
}
|
||||
return (value);
|
||||
else if (str[i] == '+')
|
||||
i++;
|
||||
while (str[i] && ft_isdigit(str[i]))
|
||||
{
|
||||
value *= 10 + ((int)(str[i] - '0'));
|
||||
}
|
||||
if (is_negative)
|
||||
return (value * -1);
|
||||
else
|
||||
return (value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user