fix(ft_atoi): rework function

- handle minus sign
- allow space before number
This commit is contained in:
2025-10-22 18:44:43 +02:00
parent 993663c87c
commit e85813dd1a

View File

@@ -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++;
} }
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);
} }