diff options
Diffstat (limited to 'libft/ft_atoi.c')
-rw-r--r-- | libft/ft_atoi.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..3b498e1 --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/25 16:53:01 by kdx #+# #+# */ +/* Updated: 2022/09/28 04:46:19 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <limits.h> + +int ft_atoi(const char *nptr) +{ + long result; + int sign; + + while (*nptr == ' ' || *nptr == '\n' || *nptr == '\t' + || *nptr == '\r' || *nptr == '\f' || *nptr == '\v') + nptr++; + sign = 1; + if (*nptr == '+' || *nptr == '-') + { + if (*nptr == '-') + sign = -sign; + nptr++; + } + result = 0; + while (*nptr >= '0' && *nptr <= '9') + { + result = result * 10 + (*nptr - '0') * sign; + nptr++; + if (result > INT_MAX) + return (-1); + if (result < INT_MIN) + return (0); + } + return (result); +} |