Add long and long long versions of ft_atoi.
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 6 Dec 2023 09:45:45 +0000 (10:45 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 6 Dec 2023 09:45:45 +0000 (10:45 +0100)
Add the functions ft_atol and ft_atoll to the file ft_atoi.c.
Add prototypes of these functions to libft.h.

ft_atoi.c
libft.h

index 881b763fc6188221f8d9f57b52d0a9efc761242f..3910863082d468450669a7ec847c21874ab01607 100644 (file)
--- 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 ac0e5b263b80ca3a62cd81a0fd0ddbd7dac83127..bc40688908907af2233c208e5d3b5d05636eee07 100644 (file)
--- 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);