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