-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
86 lines (78 loc) · 1.9 KB
/
Copy pathft_itoa.c
File metadata and controls
86 lines (78 loc) · 1.9 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: enaboule <enaboul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/28 15:09:09 by enaboule #+# #+# */
/* Updated: 2016/01/28 15:39:55 by enaboule ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static char *ft_chiffre(int n)
{
char *str;
if (n < 0)
{
if ((str = (char *)(malloc(sizeof(char) * 3))) == NULL)
return (NULL);
str[0] = '-';
str[1] = -n + '0';
str[2] = '\0';
}
else
{
if ((str = (char *)(malloc(sizeof(char) * 2))) == NULL)
return (NULL);
str[0] = n + '0';
str[1] = '\0';
}
return (str);
}
static char *ft_wrnb(int cpt, unsigned int nbr, int ng)
{
char *str;
if ((str = (char *)(malloc(sizeof(char) * cpt + ng + 1))) == NULL)
return (NULL);
str[cpt + ng] = '\0';
if (ng == 1)
cpt++;
while (cpt > 0)
{
cpt--;
str[cpt] = (nbr % 10) + '0';
nbr = nbr / 10;
}
if (ng == 1)
str[cpt] = '-';
return (str);
}
static char *ft_nb(int n)
{
unsigned int nbr;
int ng;
int cpt;
ng = 0;
cpt = 0;
if (n < 0)
{
ng = 1;
n = -n;
}
nbr = n;
while (nbr > 0)
{
nbr = nbr / 10;
cpt++;
}
return (ft_wrnb(cpt, (unsigned int)n, ng));
}
char *ft_itoa(int n)
{
if (n > -10 && n < 10)
return (ft_chiffre(n));
else
return (ft_nb(n));
}