-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_utils.c
80 lines (71 loc) · 1.64 KB
/
ft_printf_utils.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
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: llatrice <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/13 16:03:09 by llatrice #+# #+# */
/* Updated: 2021/12/25 14:49:54 by llatrice ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar(char c, int *sum)
{
write (1, &c, 1);
(*sum)++;
}
int ft_singlechar(char c)
{
write (1, &c, 1);
return (1);
}
static int ft_ilen(int num)
{
int len;
len = 0;
if (num < 0)
len++;
while (num)
{
num /= 10;
len++;
}
return (len);
}
static void ft_itoa_recurse(int num, int len, int *sum)
{
int c;
if (num > -10)
{
c = '0' - num;
write (1, &c, 1);
(*sum)++;
}
else
{
ft_itoa_recurse(num / 10, len - 1, sum);
c = '0' - num % 10;
write (1, &c, 1);
(*sum)++;
}
}
void ft_itoa(int n, int *sum)
{
int kol;
int s;
s = 0;
kol = ft_ilen(n);
if (n == 0)
{
ft_putchar('0', &s);
(*sum) += s;
return ;
}
if (n > 0)
n = -n;
else
ft_putchar('-', &s);
ft_itoa_recurse(n, kol - 1, &s);
(*sum) += s;
}