-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
85 lines (77 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
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fraqioui <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/07 11:39:06 by fraqioui #+# #+# */
/* Updated: 2022/10/20 13:50:46 by fraqioui ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
static size_t ft_size(int n)
{
int i;
i = 0;
if (n == 0)
return (1);
if (n < 0)
i++;
while (n)
{
i++;
n /= 10;
}
return (i);
}
static char *ft_strrev(char *s)
{
size_t i;
size_t j;
char tmp;
i = 0;
tmp = 0;
j = ft_strlen(s);
while (i < j / 2)
{
tmp = s[j - i - 1];
s[j - i -1] = s[i];
s[i] = tmp;
i++;
}
return (s);
}
static unsigned int ft_checksign(int num)
{
unsigned int s;
s = num;
if (num < 0)
s *= -1;
return (s);
}
char *ft_itoa(int n)
{
char *s;
size_t i;
int j;
unsigned int digit1;
i = 0;
j = 0;
i = ft_size(n);
digit1 = ft_checksign(n);
s = malloc((i + 1) * sizeof(char));
if (!s)
return (0);
if (n == 0)
s[j++] = '0';
while (digit1)
{
s[j++] = (digit1 % 10) + '0';
digit1 /= 10;
}
if (n < 0)
s[j++] = '-';
s[j] = '\0';
return (ft_strrev(s));
}