-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
68 lines (62 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ademenet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/26 12:05:26 by ademenet #+# #+# */
/* Updated: 2015/12/02 14:07:15 by ademenet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
#include "libft.h"
static long ft_digitnb(int n)
{
long size;
if (n == 0)
return (1);
size = 0;
if (n < 0)
{
size++;
n = -n;
}
while (n != 0)
{
n /= 10;
size++;
}
return (size++);
}
static int ft_sign(int n)
{
if (n < 0)
return (1);
return (0);
}
char *ft_itoa(int n)
{
long n_long;
long length;
char *fresh;
n_long = n;
length = ft_digitnb(n_long);
fresh = (char *)malloc((length + 1) * sizeof(char));
if (!fresh)
return (NULL);
fresh[length] = '\0';
length--;
if (n_long < 0)
n_long = -n_long;
while (length >= 0)
{
fresh[length] = (n_long % 10) + '0';
length--;
n_long /= 10;
}
if (ft_sign(n))
fresh[0] = '-';
return (fresh);
}