-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_int_str.c
More file actions
50 lines (49 loc) · 856 Bytes
/
print_int_str.c
File metadata and controls
50 lines (49 loc) · 856 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "main.h"
/**
* print_int_str - returns a string of chars which are int
* @num: actual integer
* @buffer: array of chars
* Return: void
*/
void print_int_str(int num, char *buffer)
{
int i = 0, j = 0, is_negative = 0, left = 0 /*right = i - 1*/;
char *temp;
temp = malloc(_strlen(buffer) * sizeof(char));
if (temp == NULL)
{
printf("Memory allocation failed");
return;
}
if (num == 0)
{
buffer[0] = '0';
buffer[1] = '\0';
return;
}
if (num < 0)
{
is_negative = 1;
num = -num;
}
while (num != 0)
{
buffer[i++] = '0' + num % 10;
num /= 10;
}
if (is_negative)
{
buffer[i++] = '-';
}
buffer[i] = '\0';
for (left = (_strlen(buffer) - 1); left >= 0; left--)
{
temp[j++] = buffer[left];
}
temp[_strlen(buffer)] = '\0';
for (i = 0; temp[i] != '\0'; i++)
{
buffer[i] = temp[i];
}
buffer[i] = '\0';
}