-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putx.c
69 lines (63 loc) · 1.72 KB
/
ft_putx.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
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmery <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/10 16:15:32 by lmery #+# #+# */
/* Updated: 2021/12/17 16:33:28 by lmery ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_error(char *base)
{
int i;
int j;
i = 0;
if (ft_strlen(base) <= 1 || !(base))
return (0);
while (base[i])
{
j = i + 1;
while (base[j])
{
if (base[i] == base[j])
return (0);
j++;
}
if ((base[i] == '+') || (base[i] == '-')
|| (base[i] <= 32) || (base [i] == 127))
return (0);
i++;
}
return (1);
}
static int ft_convert(long int nbr, char *base, int l, int *res)
{
if (nbr >= l)
{
(*res) += 1;
ft_convert((nbr / l), base, l, res);
ft_putchar(base[nbr % l]);
}
else
{
ft_putchar(base[nbr % l]);
(*res) += 1;
}
return (*res);
}
int ft_putx(unsigned int nbr, char *base)
{
int l;
unsigned int nb;
int res;
res = 0;
nb = (unsigned long int) nbr;
if (!ft_error(base))
return (0);
l = ft_strlen(base);
res += ft_convert(nb, base, l, &res);
return (res / 2);
}