This repository was archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
67 lines (60 loc) · 1.52 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahector <ahector@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 19:59:17 by ahector #+# #+# */
/* Updated: 2021/10/28 16:05:03 by ahector ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_numlen(int n)
{
int len;
len = 0;
if (n <= 0)
len = 1;
else
len = 0;
while (n != 0)
{
n = n / 10;
len++;
}
return (len);
}
static int ft_itoa_sign(int n)
{
int sign;
if (n < 0)
sign = 1;
else
sign = 0;
return (sign);
}
char *ft_itoa(int n)
{
unsigned int nbr;
char *str;
int size;
str = NULL;
size = ft_numlen(n);
if (n < 0)
nbr = -n;
else
nbr = n;
str = (char *)malloc(sizeof(char) * (size + 1));
if (!str)
return (NULL);
str[size--] = '\0';
while (size >= 0)
{
str[size--] = nbr % 10 + '0';
nbr = nbr / 10;
}
if (ft_itoa_sign(n) == 1)
str[0] = '-';
return (str);
}