diff options
Diffstat (limited to 'ft_atoi.c')
| -rw-r--r-- | ft_atoi.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/ft_atoi.c b/ft_atoi.c new file mode 100644 index 0000000..6f7bd55 --- /dev/null +++ b/ft_atoi.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/06 16:25:27 by dkaiser #+# #+# */ +/* Updated: 2024/03/06 19:41:51 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_atoi(const char *str) +{ + int result; + int i; + int posneg; + + posneg = 1; + result = 0; + i = 0; + while (str[i] >= '\t' && str[i] <= '\r' || str[i] == ' ') + { + i++; + } + if (str[i] == '-') + { + posneg = -1; + i++; + } + while (str[i] >= '0' && str[i] <= '9') + { + result = 10 * result + str[i] - '0'; + i++; + } + return (result * posneg); +} + +/* #include <stdio.h> */ +/* #include <stdlib.h> */ +/* int main() { */ +/* char str[] = " -42eaeouai"; */ +/* printf("atoi: %d\n", atoi(str)); */ +/* printf("ft_atoi: %d\n", ft_atoi(str)); */ +/* } */ |
