-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa_unsigned.c
53 lines (48 loc) · 1.39 KB
/
ft_itoa_unsigned.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttavares <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/05 14:39:24 by ttavares #+# #+# */
/* Updated: 2022/12/05 16:51:41 by ttavares ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_nbrsize(unsigned int n)
{
int size;
size = 0;
if (n == 0)
size++;
while (n > 0)
{
n = n / 10;
size++;
}
return (size);
}
char *ft_itoa_unsigned(unsigned int n)
{
char *ptr;
unsigned int size;
unsigned int nbr;
unsigned int i;
size = ft_nbrsize(n);
ptr = (char *)malloc(size + 1);
if (!ptr)
return (0);
nbr = n;
i = size - 1;
ptr[size] = '\0';
while (nbr > 0)
{
ptr[i] = nbr % 10 + '0';
nbr /= 10;
i--;
}
if (n == 0)
ptr[0] = '0';
return (ptr);
}