-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_base.c
60 lines (55 loc) · 1.54 KB
/
ft_putnbr_base.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kokaimov <kokaimov@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/19 21:36:37 by kokaimov #+# #+# */
/* Updated: 2023/11/19 21:36:37 by kokaimov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check_base(char *base)
{
int i;
int j;
if (ft_strlen(base) < 2)
return (0);
i = 0;
while (base[i])
{
if (base[i] == '+' || base[i] == '-')
return (0);
j = i + 1;
while (base[j])
{
if (base[i] == base[j])
return (0);
j++;
}
i++;
}
return (1);
}
int ft_putnbr_base(long nbr, char *base)
{
long nb;
int base_len;
int count;
if (!check_base(base))
return (0);
nb = nbr;
base_len = (int)ft_strlen(base);
count = 0;
if (nb < 0)
{
nb = -nb;
write(1, "-", 1);
count++;
}
if (nb >= base_len)
count += ft_putnbr_base(nb / base_len, base);
write(1, &base[nb % base_len], 1);
return (count + 1);
}