-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathft_itoa.c
More file actions
42 lines (39 loc) · 1.23 KB
/
ft_itoa.c
File metadata and controls
42 lines (39 loc) · 1.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mimeyer <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/21 23:06:08 by mimeyer #+# #+# */
/* Updated: 2019/05/28 10:56:42 by mimeyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int nb)
{
char *str;
long n;
int i;
n = nb;
i = ft_numlen(n);
if (!(str = (char*)malloc(sizeof(char) * (i + 1))))
return (NULL);
str[i--] = '\0';
if (n == 0)
{
str[0] = 48;
return (str);
}
if (n < 0)
{
str[0] = '-';
n = n * -1;
}
while (n > 0)
{
str[i--] = 48 + (n % 10);
n = n / 10;
}
return (str);
}