-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_unsigneditoa.c
More file actions
53 lines (47 loc) · 1.46 KB
/
ft_unsigneditoa.c
File metadata and controls
53 lines (47 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unsigneditoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Cerdelen < cerdelen@student.42wolfsburg.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/04 20:20:21 by Cerdelen #+# #+# */
/* Updated: 2021/12/04 20:20:21 by Cerdelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int digits(unsigned int n)
{
int dig;
unsigned int x;
dig = 1;
x = n;
while (x >= 10)
{
dig += 1;
x /= 10;
}
return (dig);
}
static char *stringcreate(int dig, unsigned int n, char *ptr)
{
ptr[dig] = 0;
while (dig > 0)
{
ptr[dig - 1] = (n % 10) + 48;
n /= 10;
dig--;
}
return (ptr);
}
char *ft_unsigneditoa(unsigned int n)
{
int dig;
char *ptr;
dig = digits(n);
ptr = malloc(dig + 1 * sizeof(char));
if (ptr == NULL)
return (NULL);
ptr = stringcreate(dig, n, ptr);
return (ptr);
}