-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_putnbrs.c
More file actions
62 lines (57 loc) · 1.7 KB
/
ft_putnbrs.c
File metadata and controls
62 lines (57 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldiaz-ra <ldiaz-ra@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/06 14:26:02 by ldiaz-ra #+# #+# */
/* Updated: 2023/10/12 11:26:26 by ldiaz-ra ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int putnbr(unsigned long long nbr, const char *base)
{
int base_len;
int count;
base_len = ft_strlen(base);
count = 0;
if (nbr < (unsigned long long)base_len)
{
ft_putchar_fd(*(base + (size_t)nbr % base_len), 1);
count++;
}
else
{
count += putnbr(nbr / base_len, base);
count += putnbr(nbr % base_len, base);
}
return (count);
}
int ft_putnbr_base(int nbr, const char *base)
{
int base_len;
long long int nb;
int count;
base_len = ft_strlen(base);
count = 0;
if (nbr < 0)
{
write(1, "-", 1);
count++;
nb = -((long long int)nbr);
}
else
nb = (long long int)nbr;
if (nb < base_len)
{
ft_putchar_fd(*(base + (size_t)nb % base_len), 1);
count++;
}
else
{
count += ft_putnbr_base(nb / base_len, base);
count += ft_putnbr_base(nb % base_len, base);
}
return (count);
}