-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
52 lines (47 loc) · 1.42 KB
/
ft_itoa.c
File metadata and controls
52 lines (47 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: halhashm <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/09 23:22:12 by halhashm #+# #+# */
/* Updated: 2021/10/13 00:17:37 by halhashm ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strcpy(char *s, char *d)
{
int i;
i = 0;
while (s[i])
{
d[i] = s[i];
i++;
}
d[i] = '\0';
return (d);
}
char *ft_itoa(int n)
{
char *str;
str = (char *)malloc(sizeof(char) * 2);
if (!str)
return (NULL);
if (n == -2147483648)
return (ft_strcpy(str, "-2147483648"));
if (n < 0)
{
str[0] = '-';
str[1] = '\0';
str = ft_strjoin(str, ft_itoa(-n));
}
else if (n >= 10)
str = ft_strjoin(ft_itoa(n / 10), ft_itoa(n % 10));
else if (n > 0 && n < 10)
{
str[0] = n + '0';
str[1] = '\0';
}
return (str);
}