-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_utoa.c
74 lines (68 loc) · 1.57 KB
/
ft_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rhsu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/11 14:51:38 by rhsu #+# #+# */
/* Updated: 2023/10/13 00:17:46 by rhsu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void intstr(long l, char *s, int *i)
{
if (l > 9)
{
intstr(l / 10, s, i);
s[*i] = (char)('0' + (int)(l % 10));
}
else
s[*i] = (char)('0' + (int)l);
(*i)++;
s[*i] = '\0';
}
static char *alloc_intstr(char *s)
{
size_t i;
char *r;
i = 0;
while (s[i])
i++;
r = (char *)malloc(sizeof(char) * (i + 1));
if (r != NULL)
{
i = 0;
while (s[i])
{
r[i] = s[i];
i++;
}
r[i] = '\0';
}
return (r);
}
char *ft_utoa(unsigned int n)
{
long l;
char s[20];
int i;
l = (long)n;
i = 0;
if (l == 0)
{
s[0] = '0';
s[1] = '\0';
}
else
{
if (l < 0)
{
l = l * (-1);
s[0] = '-';
i = 1;
}
intstr(l, s, &i);
}
return (alloc_intstr(s));
}