-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_utils3.c
79 lines (69 loc) · 1.06 KB
/
ft_printf_utils3.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
#include "ft_printf.h"
size_t ft_intlen(intmax_t n)
{
size_t len;
len = 0;
if (!n)
len++;
while (n)
{
n /= 10;
len++;
}
return (len);
}
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}
char *ft_strdup(const char *s1)
{
char *str;
int i;
i = 0;
if (!(str = (char *)malloc(sizeof(char) * (ft_strlen(s1) + 1))))
return (NULL);
while (s1[i])
{
str[i] = (char)s1[i];
i++;
}
str[i] = '\0';
return (str);
}
char *ft_itoa_base(uintmax_t n, char *base)
{
char *str;
int num_len;
int base_len;
num_len = ft_intlen_base(n, base);
base_len = ft_strlen(base);
if (!(str = ft_calloc((num_len + 1), sizeof(char))))
return (NULL);
str[num_len] = '\0';
while (num_len)
{
str[--num_len] = base[n % base_len];
n /= base_len;
}
return (str);
}
char *ft_uitoa(uintmax_t n)
{
char *str;
int num_len;
num_len = ft_uintlen(n);
if (!(str = ft_calloc((num_len + 1), sizeof(char))))
return (NULL);
str[num_len] = '\0';
while (num_len)
{
str[--num_len] = n % 10 + 48;
n /= 10;
}
return (str);
}