-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
76 lines (66 loc) · 1.75 KB
/
ft_itoa.c
File metadata and controls
76 lines (66 loc) · 1.75 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_itoa.c :+: :+: */
/* +:+ */
/* By: dreijans <dreijans@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/10/27 17:43:26 by dreijans #+# #+# */
/* Updated: 2022/11/14 14:29:05 by dreijans ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static int howmuch(int a)
{
int i;
i = 0;
if (a <= 0)
i = i + 1;
while (a != 0)
{
a = a / 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
char *str;
int index;
long num;
num = n;
index = howmuch(num);
str = ft_calloc(index + 1, sizeof (char));
if (str == NULL)
return (NULL);
index--;
if (num == 0)
str[0] = '0';
if (num < 0)
{
num = num * -1;
str[0] = '-';
}
while (num != 0)
{
str[index] = (num % 10) + 48;
num = num / 10;
index--;
}
return (str);
}
/*
Parameters n:
the integer to convert.
Return value:
The string representing the integer.
NULL if the allocation fails.
External functs:
malloc
Description:
Allocates (with malloc(3)) and returns a string
representing the integer received as an argument.
Negative numbers must be handled.
minus before i???
<= 0 = i + 1 because you want space to print minus or a zero/
*/