-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
51 lines (46 loc) · 1.4 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nbarreir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/18 17:28:02 by nbarreir #+# #+# */
/* Updated: 2021/04/21 17:16:13 by nbarreir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void conver_putnbr(int c, char *str, long int i)
{
unsigned int m;
m = c;
if (c < 0)
{
str[0] = '-';
m = (m * (-1));
}
if (m >= 10)
conver_putnbr((m / 10), str, (i - 1));
str[i] = (m % 10) + '0';
}
char *ft_itoa(int n)
{
char *str;
long int j;
long int count;
j = n;
count = 0;
if (j <= 0)
count++;
while (j)
{
j = (j / 10);
count++;
}
str = malloc(sizeof(char) * (count + 1));
if (!str)
return (NULL);
str[count] = 0;
conver_putnbr(n, str, (count - 1));
return (str);
}