-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_utils.c
96 lines (86 loc) · 1.78 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ygunay <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/17 18:05:28 by ygunay #+# #+# */
/* Updated: 2022/08/18 15:34:27 by ygunay ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(char c)
{
write(1, &c, 1);
return (1);
}
int ft_print_str(const char *s1)
{
int i;
i = 0;
if (s1 == 0)
{
write(1, "(null)", 6);
return (6);
}
while (s1[i])
{
write (1, &s1[i], 1);
i++;
}
return (i);
}
static size_t ft_get_len(int n)
{
size_t len;
if (n == 0)
return (1);
len = 0;
if (n < 0)
{
n *= -1;
len++;
}
while (n)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
size_t len;
long int nb;
char *result;
len = ft_get_len(n);
nb = n;
result = malloc(sizeof(char) * len + 1);
if (!result)
return (NULL);
if (n < 0)
{
result[0] = '-';
nb *= -1;
}
if (n == 0)
result[0] = '0';
result[len--] = '\0';
while (nb)
{
result[len] = nb % 10 + '0';
nb /= 10;
len--;
}
return (result);
}
int ft_print_nbr(int nbr)
{
int len;
char *nb;
nb = ft_itoa(nbr);
len = ft_print_str(nb);
free(nb);
return (len);
}