-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_for_ui.c
59 lines (52 loc) · 1.48 KB
/
ft_itoa_for_ui.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_for_ui.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vmiachko <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/18 16:46:36 by vmiachko #+# #+# */
/* Updated: 2018/02/19 19:01:15 by vmiachko ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
static int ft_count_digits_ui(size_t n)
{
int i;
i = 0;
while (n != 0)
{
n /= 10;
++i;
}
return (i);
}
static char *create_string(char *res, size_t n)
{
int neg;
int l;
l = ft_count_digits_ui(n) - 1;
neg = 1;
res[l + 1] = '\0';
while (l > -1)
{
res[l--] = n % 10 + 48;
n /= 10;
}
if (neg < 0)
res[0] = '-';
return (res);
}
char *ft_itoa_ui(size_t n)
{
char *res;
res = (char *)ft_memalloc((ft_count_digits_ui(n) + 2) * sizeof(char));
if (res == NULL)
return (NULL);
if (n == 0)
{
res[0] = '0';
return (res);
}
return (create_string(res, n));
}