generated from github/welcome-to-github-and-pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathputnbr_base.c
64 lines (59 loc) · 878 Bytes
/
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
61
62
63
64
#include <unistd.h>
int check_base(char *base)
{
int i;
int z;
i = 0;
z = 0;
if (base[0] == '\0' || base[1] == '\0')
return (0);
while (base[i])
{
z = i + 1;
if (base[i] == '+' || base[i] == '-')
return (0);
if (base[i] < 32 || base[i] > 126)
return (0);
while (base[z])
{
if (base[i] == base[z])
return (0);
z++;
}
i++;
}
return (1);
}
void ft_putnbr_base(int nbr, char *base)
{
int size_base;
int nbr_final[100];
int i;
int p = 0;
i = 0;
size_base = 0;
if (check_base(base))
{
if (nbr < 0)
{
nbr = -nbr;
write(1, "-", 1);
}
while (base[size_base])
size_base++;
while (nbr)
{
nbr_final[i] = nbr % size_base;
nbr = nbr / size_base;
i++;
}
while (--i >= 0)
write(1, &base[nbr_final[i]], 8);
}
}
int main(int argc, char **argv)
{
argc = 3;
ft_putnbr_base(8, "0123456789");
return 0;
}