-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_utils.c
More file actions
65 lines (57 loc) · 1.43 KB
/
ft_printf_utils.c
File metadata and controls
65 lines (57 loc) · 1.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maelgini <maelgini@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/08 13:49:01 by maelgini #+# #+# */
/* Updated: 2025/01/27 17:30:50 by maelgini ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}
size_t ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}
int get_num_size(int n)
{
int len;
len = 1;
while (n / 10)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
char *str;
long nb;
int len;
nb = n;
len = get_num_size(n) + (n < 0);
if (n < 0)
nb = -nb;
str = malloc(len + 1);
if (!str)
return (NULL);
str[len] = '\0';
while (--len >= 0)
{
str[len] = nb % 10 + '0';
nb /= 10;
}
if (n < 0)
str[0] = '-';
return (str);
}