Skip to content

Commit 4d3139f

Browse files
author
Jean-Michel Gigault
committed
added: ft_atoll ft_lltoa
1 parent df338b5 commit 4d3139f

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ SRCS = \
6060
ft_arr_/ft_array_push_front.c \
6161
ft_arr_/ft_array_push_index.c \
6262
ft_ito_/ft_itoa.c \
63+
ft_ito_/ft_lltoa.c \
6364
ft_math_/ft_powi.c \
6465
ft_mem_/ft_memalloc.c \
6566
ft_mem_/ft_memdel.c \
@@ -164,6 +165,7 @@ SRCS = \
164165
recodes/ft_strcmp.c \
165166
recodes/ft_strncmp.c \
166167
recodes/ft_atoi.c \
168+
recodes/ft_atoll.c \
167169
recodes/ft_isalpha.c \
168170
recodes/ft_isdigit.c \
169171
recodes/ft_isalnum.c \

incs/libft.h

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ int ft_array_push_index(char ***array, char const *value,
2121
*/
2222

2323
char *ft_itoa(int n);
24+
char *ft_lltoa(long long n);
2425

2526
/*
2627
** Mathematics
@@ -108,6 +109,7 @@ void *ft_memchr(const void *s, int c, size_t n);
108109
int ft_memcmp(const void *s1, const void *s2, size_t n);
109110

110111
int ft_atoi(const char *str);
112+
long long ft_atoll(const char *str);
111113

112114
int ft_isalpha(int c);
113115
int ft_isdigit(int c);

srcs/ft_ito_/ft_lltoa.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <string.h>
2+
#include "libftprintf.h"
3+
4+
static void ft_lltoa_getlength(size_t *len, long long *n)
5+
{
6+
long long n2;
7+
8+
*len = 0;
9+
n2 = *n;
10+
while (n2 != 0)
11+
{
12+
(*len)++;
13+
n2 = n2 / 10;
14+
}
15+
}
16+
17+
char *ft_lltoa(long long n)
18+
{
19+
char *str;
20+
size_t len;
21+
unsigned long long n2;
22+
23+
ft_lltoa_getlength(&len, &n);
24+
n2 = (n < 0 ? -n : n);
25+
str = ft_strnew(len + 1);
26+
if (!str)
27+
return (NULL);
28+
while (len > 0)
29+
{
30+
str[len - (n < 0 ? 0 : 1)] = (n2 % 10) + 48;
31+
n2 = n2 / 10;
32+
len--;
33+
}
34+
if (n < 0)
35+
str[0] = '-';
36+
else if (n == 0)
37+
str[0] = '0';
38+
return (str);
39+
}

srcs/recodes/ft_atoll.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <string.h>
2+
#include "libftprintf.h"
3+
4+
long long ft_atoll(char const *str)
5+
{
6+
short neg;
7+
long long res;
8+
9+
neg = 0;
10+
res = 0;
11+
while (ft_isspace(*str))
12+
str++;
13+
if (*str == '-' || *str == '+')
14+
{
15+
if (*str == '-')
16+
neg = 1;
17+
str++;
18+
}
19+
while (*str && ft_isdigit(*str))
20+
{
21+
res = res * 10;
22+
res += (*str - 48);
23+
str++;
24+
}
25+
return ((neg == 1 ? -res : res));
26+
}

0 commit comments

Comments
 (0)