-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
63 lines (58 loc) · 1.43 KB
/
ft_itoa.c
File metadata and controls
63 lines (58 loc) · 1.43 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oishchen <oishchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/14 18:53:51 by oishchen #+# #+# */
/* Updated: 2025/03/24 10:25:49 by oishchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
int n_size(int n)
{
int len;
long long a;
len = 1;
a = n;
if (a < 0)
{
len++;
a = a * (-1);
}
while (a >= 10)
{
a /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int size;
char *res;
long long a;
a = n;
size = n_size(n);
res = (char *)malloc((size + 1) * sizeof(char));
if (!res)
return (NULL);
if (a < 0)
{
res[0] = '-';
a *= -1;
}
else if (a == 0)
res[0] = '0';
res[size] = '\0';
size--;
while (a != 0)
{
res[size] = (a % 10) + '0';
a /= 10;
size--;
}
return (res);
}