-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_btoi.c
More file actions
34 lines (31 loc) · 675 Bytes
/
_btoi.c
File metadata and controls
34 lines (31 loc) · 675 Bytes
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
#include "main.h"
/**
* _btoi - converts numbers from int to string.
*
* @num: number to be processed.
*
* Return: the number in form of char *.
*/
char *_btoi(int num)
{
int counter = 0, index, tempi, digit, base = 1, last;
char *number;
last = num % 10;
counter += num_of_digits(num);
number = malloc(sizeof(char) * counter + 1);
if (counter > 1)
{
for (index = 1; index < counter; index++)
{
for (tempi = counter - index; tempi != 0; tempi--)
base = base * 10;
digit = num / (base);
digit = digit % 10;
number[index - 1] = digit + '0';
base = 1;
}
}
number[counter - 1] = last + '0';
number[counter] = '\0';
return (number);
}