diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-24 18:57:19 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-24 18:57:19 +0200 |
| commit | 475449dd4b1f3308bac6f72c34d87812216a0738 (patch) | |
| tree | 319a76f0e87b041818156f2d1dfbbc6a7dfacf06 /exam02/rendu/ft_atoi | |
| parent | 83edb77d74bb339f3e1324a51039c78ac503db90 (diff) | |
| download | piscine-475449dd4b1f3308bac6f72c34d87812216a0738.tar.gz piscine-475449dd4b1f3308bac6f72c34d87812216a0738.tar.bz2 piscine-475449dd4b1f3308bac6f72c34d87812216a0738.zip | |
exam02
Diffstat (limited to 'exam02/rendu/ft_atoi')
| -rwxr-xr-x | exam02/rendu/ft_atoi/ft_atoi.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/exam02/rendu/ft_atoi/ft_atoi.c b/exam02/rendu/ft_atoi/ft_atoi.c new file mode 100755 index 0000000..eda0f8d --- /dev/null +++ b/exam02/rendu/ft_atoi/ft_atoi.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: exam <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 17:46:19 by exam #+# #+# */ +/* Updated: 2019/07/19 18:03:23 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int pow10(int exponent) +{ + int acc; + + acc = 1; + while (exponent > 0) + { + acc *= 10; + exponent--; + } + return (acc); +} + + +int ft_atoi(const char *str) +{ + int nb; + int i; + int j; + int is_negative; + + nb = 0; + while (*str == ' ' | *str == '\t' || *str == '\n' || *str == '\v' + || *str == '\f' || *str == '\r') + str++; + is_negative = 0; + if (*str == '-' || *str == '+') + { + if (*str == '-') + is_negative = 1; + str++; + } + j = 0; + while (str[j] >= '0' && str[j] <= '9') + j++; + j--; + i = 0; + while (j >= 0) + { + nb += (str[j] - '0') * pow10(i); + i++; + j--; + } + if (is_negative) + nb = -nb; + return (nb); +} |
