-
Notifications
You must be signed in to change notification settings - Fork 0
/
utoa.c
52 lines (46 loc) · 1.43 KB
/
utoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alida-si <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 08:26:38 by alida-si #+# #+# */
/* Updated: 2021/11/09 23:21:24 by alida-si ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *n_convert(char *str, size_t size, unsigned int num)
{
str[size] = '\0';
while (size--)
{
str[size] = (num % 10) + 48;
num /= 10;
}
return (str);
}
static size_t nb_size(unsigned int num)
{
size_t len;
len = 1;
num /= 10;
while (num)
{
num /= 10;
len++;
}
return (len);
}
int utoa(unsigned int n)
{
size_t n_digits;
char *str;
n_digits = nb_size(n);
str = malloc(sizeof(char) * (n_digits + 1));
if (str == NULL)
return (0);
ft_putstr_fd(n_convert(str, n_digits, n), 1);
free (str);
return (n_digits);
}