Files
libft/ft_atoi.c
Loïc GUEZO e85813dd1a fix(ft_atoi): rework function
- handle minus sign
- allow space before number
2025-10-22 18:44:43 +02:00

30 lines
397 B
C

#include "libft.h"
int ft_atoi(const char *str)
{
int value;
size_t i;
int is_negative;
value = 0;
i = 0;
is_negative = 0;
while (str[i] == ' ')
i++;
if (str[i] == '-')
{
is_negative = 1;
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);
}