-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_utils.c
More file actions
89 lines (79 loc) · 1.67 KB
/
ft_printf_utils.c
File metadata and controls
89 lines (79 loc) · 1.67 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erigolon <erigolon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 11:50:33 by erigolon #+# #+# */
/* Updated: 2023/04/26 12:19:52 by erigolon ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putcharf(char c)
{
write(1, &c, 1);
return (1);
}
size_t ft_strlen(const char *s)
{
size_t count;
count = 0;
while (s[count] != '\0')
count++;
return (count);
}
int ft_putstrf(char *str)
{
int i;
if (!str)
{
write(1, "(null)", 6);
return (6);
}
i = 0;
while (str[i])
{
ft_putcharf(str[i]);
i++;
}
return (i);
}
int num_len(int n)
{
int len;
len = 0;
if (n <= 0)
len++;
while (n)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
char *str;
int len;
long int nb;
nb = n;
len = num_len(nb);
str = (char *)malloc(len + 1);
if (!str)
return (0);
str[0] = '0';
str[len] = '\0';
if (nb < 0)
{
str[0] = '-';
nb = nb * -1;
}
while (nb > 0)
{
len--;
str[len] = nb % 10 + 48;
nb = nb / 10;
}
return (str);
}