-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
84 lines (76 loc) · 1.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atabiti <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/17 07:57:01 by atabiti #+# #+# */
/* Updated: 2021/11/17 15:29:19 by atabiti ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int nlenght(long nb)
{
int len;
len = 0;
if (nb < 0)
nb = -nb;
len++;
while (nb > 0)
{
nb = nb / 10;
len++;
}
return (len);
}
static void reversit(char *string)
{
char c;
long i;
long j;
i = 0;
j = ft_strlen(string) - 1;
while (i < j)
{
c = string[i];
string[i] = string[j];
string[j] = c;
i++;
j--;
}
}
static void *converter(int a, char *s3)
{
long g;
size_t index;
g = a;
index = 0;
if (g == 0)
{
s3[index++] = '0';
s3[index] = '\0';
return (s3);
}
if (g < 0)
g = -g;
while (g > 0)
{
s3[index++] = g % 10 + '0';
g = g / 10;
}
if (a < 0)
s3[index++] = '-';
s3[index] = '\0';
return (0);
}
char *ft_itoa(int n)
{
char *ptr;
ptr = (char *) malloc (sizeof (char) * (nlenght(n) + 1));
if (!ptr)
return (NULL);
converter(n, ptr);
reversit(ptr);
return (ptr);
}