-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_iutoa_bonus.c
63 lines (56 loc) · 1.59 KB
/
ft_iutoa_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_iutoa_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edi-marc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/02 16:43:59 by edi-marc #+# #+# */
/* Updated: 2021/08/05 15:53:07 by edi-marc ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: libft
** SYNOPSIS: convert an unsigned int to ASCII
**
** DESCRIPTION:
** The iutoa() function converts an unsigned int to ASCII,
** returns the number represented as ASCII string,
** NULL otherwise.
*/
#include "libft.h"
static size_t count_digits(unsigned int n)
{
size_t num;
num = 0;
if (!n)
num = 1;
while (n)
{
n = n / 10;
num++;
}
return (num);
}
static void convert(unsigned int n, char *p, size_t digits)
{
if (!n)
*p = 48;
while (n)
{
p[digits - 1] = n % 10 + '0';
n = n / 10;
digits--;
}
}
char *ft_iutoa(unsigned int n)
{
char *p;
size_t digits;
p = NULL;
digits = count_digits(n);
p = ft_calloc(digits + 1, sizeof(*p));
if (p)
convert(n, p, digits);
return (p);
}