-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_base_len.c
64 lines (58 loc) · 1.65 KB
/
ft_putnbr_base_len.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
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lauger <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/20 11:06:28 by lauger #+# #+# */
/* Updated: 2023/11/16 08:22:57 by lauger ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int check_base(char *base);
int ft_putnbr_base_len(unsigned long int nbr, char *base)
{
int cnt_char;
int cnt;
unsigned long int l_nbr;
cnt = 0;
cnt_char = 0;
l_nbr = (unsigned long int)nbr;
if (check_base(base) == 0)
return (0);
while (base[cnt])
cnt++;
if (l_nbr < (unsigned long int)cnt)
{
write(1, &base[l_nbr], 1);
cnt_char++;
}
else
{
cnt_char += ft_putnbr_base_len(l_nbr / cnt, base);
cnt_char += ft_putnbr_base_len(l_nbr % cnt, base);
}
return (cnt_char);
}
static int check_base(char *base)
{
int i;
int j;
i = 0;
j = 0;
while (base[i] != '\0')
{
j = i +1;
while (base[j] != '\0')
{
if (base[i] == base[j] || (base[i] == '-' || base[i] == '+'))
{
return (0);
}
j++;
}
i++;
}
return (1);
}