-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
67 lines (60 loc) · 1.54 KB
/
ft_itoa.c
File metadata and controls
67 lines (60 loc) · 1.54 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
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldiaz-ra <ldiaz-ra@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/26 11:54:58 by ldiaz-ra #+# #+# */
/* Updated: 2023/09/28 10:47:24 by ldiaz-ra ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long long check_abs(long long n)
{
long long nb;
nb = 1;
if (n < 0)
nb *= -n;
else
nb *= n;
return (nb);
}
static int check_len(int n)
{
int len;
len = 0;
if (n < 1)
len++;
while (n)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int sign;
int len;
char *memory;
unsigned int num;
len = 0;
sign = 0;
if (n < 0)
sign = 1;
len = check_len(n);
memory = (char *)malloc(sizeof(char) * (len + 1));
if (!memory)
return (NULL);
*(memory + len) = '\0';
num = check_abs(n);
while (len--)
{
*(memory + len) = (num % 10) + '0';
num = num / 10;
}
if (sign)
*memory = 45;
return (memory);
}