This repository has been archived by the owner on Aug 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_u.c
60 lines (55 loc) · 1.45 KB
/
ft_itoa_u.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_u.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mkristie <kukinpower@ya.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/10 20:22:52 by mkristie #+# #+# */
/* Updated: 2020/06/28 05:35:47 by mkristie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_intlen_u_itoa(int num)
{
int numlen;
numlen = 0;
if (num == 0)
{
numlen = 1;
}
if (num < 0)
{
num = -1 * num;
}
while (num != 0)
{
num = num / 10;
numlen++;
}
return (numlen);
}
char *ft_itoa_u(int n)
{
int nlen;
char *s;
long num;
nlen = 0;
num = (long)n;
nlen = ft_intlen_u_itoa(n);
if (!(s = malloc(nlen + 1)))
return (NULL);
s[nlen] = '\0';
if (num == 0)
s[0] = '0';
if (num < 0)
num = -num;
nlen--;
while (nlen >= 0 && num != 0)
{
s[nlen] = num % 10 + '0';
num = num / 10;
nlen--;
}
return (s);
}