-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
68 lines (62 loc) · 1.57 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
57
58
59
60
61
62
63
64
65
66
67
68
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: avaliull <avaliull@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:44:18 by avaliull #+# #+# */
/* Updated: 2024/10/16 17:30:46 by avaliull ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static int lenctr(long n)
{
int len;
len = 0;
while (n)
{
len++;
n /= 10;
}
return (len);
}
static void itoaer(char *convstr, long tmp_int)
{
*convstr = '\0';
if (tmp_int == 0)
{
*(convstr - 1) = '0';
return ;
}
while (tmp_int)
{
convstr--;
*convstr = tmp_int % 10 + 48;
tmp_int /= 10;
}
}
char *ft_itoa(int n)
{
char nlen;
long tmp_int;
char *convstr;
nlen = 0;
tmp_int = n;
if (n < 0)
{
tmp_int = -tmp_int;
nlen++;
}
if (n == 0)
nlen = 1;
else
nlen += lenctr(tmp_int);
convstr = (char *) malloc((nlen + 1) * sizeof(char));
if (!convstr)
return (NULL);
if (n < 0)
*convstr = '-';
itoaer(convstr + nlen, tmp_int);
return (convstr);
}