-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_uitoa.c
More file actions
68 lines (59 loc) · 1.59 KB
/
ft_uitoa.c
File metadata and controls
68 lines (59 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_uitoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sgarigli <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 10:07:09 by sgarigli #+# #+# */
/* Updated: 2023/10/31 10:07:12 by sgarigli ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t ft_10pow(int exp)
{
size_t pow;
pow = 1;
while (exp > 1)
{
pow *= 10;
exp--;
}
return (pow);
}
static size_t ft_unlen(unsigned int n)
{
size_t len;
len = 1;
while (n / 10)
{
n /= 10;
len++;
}
return (len);
}
static void ft_ufilldest(char *dest, size_t len, unsigned long int nmb)
{
size_t i;
i = 0;
while (i < len)
{
dest[i] = (nmb / (ft_10pow(len - i))) + '0';
nmb = nmb % (ft_10pow(len - i));
i++;
}
dest[i] = 0;
}
char *ft_uitoa(unsigned int n)
{
size_t len;
char *dest;
unsigned long int nmb;
nmb = n;
len = ft_unlen(n);
dest = (char *)malloc(sizeof(char) * (len + 1));
if (!dest)
return (NULL);
ft_ufilldest(dest, len, nmb);
return (dest);
}