-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
52 lines (47 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mle-roy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/25 13:11:35 by mle-roy #+# #+# */
/* Updated: 2015/01/31 21:56:45 by mle-roy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_filltemp(char *temp, int *l, int n)
{
char c;
while (n != 0)
{
if (n < 0)
c = '0' - (n % 10);
else
c = (n % 10) + '0';
temp[(*l)--] = c;
n = n / 10;
}
}
char *ft_itoa(int n)
{
char *newc;
char temp[11];
int l;
int neg;
l = 10;
neg = 0;
ft_strcpy(temp, "00000000000");
if (n == 0)
l--;
else
{
if (n < 0)
neg = 1;
ft_filltemp(temp, &l, n);
if (neg == 1)
temp[l--] = '-';
}
newc = ft_strsub(temp, (l + 1), ft_strlen(&temp[l]));
return (newc);
}