-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
56 lines (51 loc) · 1.4 KB
/
ft_itoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atoof <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/11 18:50:57 by atoof #+# #+# */
/* Updated: 2022/11/14 16:36:12 by atoof ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int num_len(int n)
{
int count;
count = 0;
if (n == 0)
count = 1;
if (n < 0)
count++;
while (n)
{
n = n / 10;
count++;
}
return (count);
}
char *ft_itoa(int n)
{
long num;
char *s;
int numlen;
num = (long)n;
numlen = num_len(n);
s = (char *)malloc(sizeof(char) * (numlen + 1));
if (!s)
return (NULL);
s[numlen--] = '\0';
if (num < 0)
{
num = -num;
s[0] = '-';
}
while (num >= 10)
{
s[numlen--] = num % 10 + '0';
num = num / 10;
}
s[numlen] = num + '0';
return (s);
}