From: Lukas Jiriste Date: Wed, 6 Dec 2023 09:45:45 +0000 (+0100) Subject: Add long and long long versions of ft_atoi. X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=979c7eed8d16d89f0a0241d6124a89ea39f2ad94;p=Libft.git Add long and long long versions of ft_atoi. Add the functions ft_atol and ft_atoll to the file ft_atoi.c. Add prototypes of these functions to libft.h. --- diff --git a/ft_atoi.c b/ft_atoi.c index 881b763..3910863 100644 --- a/ft_atoi.c +++ b/ft_atoi.c @@ -35,3 +35,51 @@ int ft_atoi(const char *nptr) } return (sign * res); } + +long ft_atol(const char *nptr) +{ + long res; + int sign; + + res = 0; + sign = 1; + while (ft_isspace(*nptr)) + ++nptr; + if ((*nptr == '+' || *nptr == '-')) + { + if (*nptr == '-') + sign *= -1; + ++nptr; + } + while (ft_isdigit(*nptr)) + { + res *= 10; + res += *nptr - '0'; + ++nptr; + } + return (sign * res); +} + +long long ft_atoll(const char *nptr) +{ + long long res; + int sign; + + res = 0; + sign = 1; + while (ft_isspace(*nptr)) + ++nptr; + if ((*nptr == '+' || *nptr == '-')) + { + if (*nptr == '-') + sign *= -1; + ++nptr; + } + while (ft_isdigit(*nptr)) + { + res *= 10; + res += *nptr - '0'; + ++nptr; + } + return (sign * res); +} diff --git a/libft.h b/libft.h index ac0e5b2..bc40688 100644 --- a/libft.h +++ b/libft.h @@ -50,6 +50,8 @@ int ft_toupper(int c); int ft_tolower(int c); int ft_atoi(const char *nptr); +long ft_atol(const char *nptr); +long long ft_atoll(const char *nptr); char *ft_substr(const char *s, unsigned int start, size_t len); char *ft_strjoin(const char *s1, const char *s2);