diff options
Diffstat (limited to 'c11/ex05/helper.c')
| -rw-r--r-- | c11/ex05/helper.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/c11/ex05/helper.c b/c11/ex05/helper.c new file mode 100644 index 0000000..8f370de --- /dev/null +++ b/c11/ex05/helper.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/17 07:53:07 by cacharle #+# #+# */ +/* Updated: 2019/07/17 07:53:59 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "include.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putnbr(int nb) +{ + unsigned int p_nb; + + p_nb = nb; + if (nb < 0) + { + write(1, "-", 1); + p_nb = -nb; + } + if (p_nb > 9) + ft_putnbr(p_nb / 10); + ft_putchar(p_nb % 10 + '0'); +} + +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; + int j; + + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + is_negative = 0; + while (*str == '-' || *str == '+') + { + if (*str == '-') + is_negative = !is_negative; + str++; + } + nb = 0; + i = 0; + while (str[i] >= '0' && str[i] <= '9') + i++; + j = 0; + while (str[j] >= '0' && str[j] <= '9') + nb += pow10(--i) * (str[j++] - '0'); + if (is_negative) + nb = -nb; + return (nb); +} |
