-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
54 lines (49 loc) · 1.49 KB
/
ft_itoa.c
File metadata and controls
54 lines (49 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xvan-ham <xvan-ham@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/16 12:37:38 by xvan-ham #+# #+# */
/* Updated: 2019/11/27 19:24:24 by xvan-ham ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_size(unsigned int un)
{
unsigned int size;
size = 1;
while (un >= 10)
{
un /= 10;
size++;
}
return (size);
}
char *ft_itoa(int n)
{
char *str;
unsigned int un;
unsigned int i;
unsigned int size;
if (n < 0)
un = (unsigned int)(n * -1);
else
un = (unsigned int)n;
size = ft_size(un);
if (!(str = (char*)malloc(sizeof(char) * (size + 1 + (n < 0 ? 1 : 0)))))
return (NULL);
i = 0;
if (n < 0 && (str[i] = '-'))
size++;
i = size - 1;
while (un >= 10)
{
str[i--] = (char)(un % 10 + '0');
un /= 10;
}
str[i] = (char)(un % 10 + '0');
str[size] = 0;
return (str);
}