From d732047d6681b1f64f7907d1c0413abdaab76050 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 6 Jul 2019 08:42:30 +0200 Subject: c03 and begin c04 --- c04/ex03/ft_atoi.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 c04/ex03/ft_atoi.c (limited to 'c04/ex03') diff --git a/c04/ex03/ft_atoi.c b/c04/ex03/ft_atoi.c new file mode 100644 index 0000000..0d4247b --- /dev/null +++ b/c04/ex03/ft_atoi.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:31:38 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:36:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int pow10(int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent > 0) + { + accumulator *= 10; + exponent--; + } + return (accumulator); +} + +int ft_atoi(char *str) +{ + int is_negative; + int nb; + int i; + + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + while (*str == '-' || *str == '+') + { + if (*str == '-') + is_negative = !is_negative; + str++; + } + i = 0; + while (str[i] >= '0' && str[i] <= '9') + { + nb += pow10(i) * (str[i] - '0'); + i++; + } + if (is_negative) + nb = -nb; + return (nb); +} -- cgit