diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-16 12:58:13 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-16 12:58:13 +0200 |
| commit | 14914f50c3de6c5444e13cf67db064b03c1c90ef (patch) | |
| tree | 3ecf5a41b4541980ba0287a431681cd5ae25ad84 /exam00/rendu/ft_atoi | |
| parent | 217bcb0d4e3ba60604921cb40d5a11a64f93cfc7 (diff) | |
| download | piscine-14914f50c3de6c5444e13cf67db064b03c1c90ef.tar.gz piscine-14914f50c3de6c5444e13cf67db064b03c1c90ef.tar.bz2 piscine-14914f50c3de6c5444e13cf67db064b03c1c90ef.zip | |
c09 passed, c10 start, exam00 and exam01
Diffstat (limited to 'exam00/rendu/ft_atoi')
| -rwxr-xr-x | exam00/rendu/ft_atoi/ft_atoi.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/exam00/rendu/ft_atoi/ft_atoi.c b/exam00/rendu/ft_atoi/ft_atoi.c new file mode 100755 index 0000000..81f0706 --- /dev/null +++ b/exam00/rendu/ft_atoi/ft_atoi.c @@ -0,0 +1,60 @@ +char ctoi(char c) +{ + return (c - '0'); +} + +int ft_strlen(const char *str) +{ + int counter; + + counter = 0; + while (*str >= '0' && *str <= '9') + { + counter++; + str++; + } + return (counter); +} + +int pow10(int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent > 0) + { + accumulator *= 10; + exponent--; + } + return (accumulator); +} + +int ft_atoi(const char *str) +{ + int nb; + int i; + int is_negative; + + while (*str == ' ' || *str == '\t'|| *str == '\n' + || *str == '\v'|| *str == '\f'|| *str == '\r') + str++; + if (*str == '-') + is_negative = 1; + else + is_negative = 0; + if (*str == '-' || *str == '+') + { + str++; + } + nb = 0; + i = ft_strlen(str) - 1; + while (*str >= '0' && *str <= '9') + { + nb += pow10(i) * ctoi(*str); + i--; + str++; + } + if (is_negative) + nb = -nb; + return (nb); +} |
