diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-06 08:42:30 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-06 08:42:30 +0200 |
| commit | d732047d6681b1f64f7907d1c0413abdaab76050 (patch) | |
| tree | c2123d55a7431de9bb55371dcc78c955da607c6f /c04/ex03 | |
| parent | af8435d40cdb8e7871ff004fb21382c236f9bd0f (diff) | |
| download | piscine-d732047d6681b1f64f7907d1c0413abdaab76050.tar.gz piscine-d732047d6681b1f64f7907d1c0413abdaab76050.tar.bz2 piscine-d732047d6681b1f64f7907d1c0413abdaab76050.zip | |
c03 and begin c04
Diffstat (limited to 'c04/ex03')
| -rw-r--r-- | c04/ex03/ft_atoi.c | 50 |
1 files changed, 50 insertions, 0 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} |
