-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
31 lines (28 loc) · 1.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bannabel <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 19:38:27 by bannabel #+# #+# */
/* Updated: 2019/06/26 20:03:13 by bannabel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
size_t count;
char *res;
int flag;
count = ft_countdigits(n);
flag = ft_isneg(n);
if (!(res = (char*)malloc(sizeof(char) * count + 1 + flag)))
return (NULL);
res[count + flag] = 0;
if (n < 0)
res[0] = '-';
while (count-- && (res[count + flag] = (n % 10) * (flag ? -1 : 1) + '0'))
n /= 10;
return (res);
}