-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
98 lines (87 loc) · 2.21 KB
/
ft_itoa.c
File metadata and controls
98 lines (87 loc) · 2.21 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stdi-pum <stdi-pum@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 17:11:05 by stdi-pum #+# #+# */
/* Updated: 2023/12/07 21:08:13 by stdi-pum ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// 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.
// take a number and tranform it in a string char.
char *ft_writestring(char *str, long int n, int count, int sign)
{
str[count--] = '\0';
if (n == 0)
str[0] = '0';
while (n != 0)
{
str[count] = (n % 10) + 48;
n /= 10;
count --;
}
if (sign)
str[0] = '-';
return (str);
}
int ft_count(long int number)
{
int count;
count = 0;
if (number < 0)
{
number *= -1;
count++;
}
while (number > 0)
{
number = number / 10;
count++;
}
return (count);
}
char *ft_itoa(int n)
{
long int number;
char *str;
int sign;
int count;
long int tmp;
number = n;
if (number < 0)
tmp = number * -1;
else
tmp = number;
sign = 0;
if (number < 0)
sign = 1;
count = ft_count(number);
number = tmp;
str = (char *)malloc((count + 1) * sizeof(char));
if (str == NULL)
return (NULL);
ft_writestring(str, number, count, sign);
return (str);
}
// #include <stdio.h>
// int main(void)
// {
// int n = 0;
// char *num;
// num = ft_itoa (n);
// printf("the number is:%s", num);
// return (0);
// }