-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmore_helper_func.c
108 lines (98 loc) · 1.83 KB
/
more_helper_func.c
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
99
100
101
102
103
104
105
106
107
108
#include "main.h"
#include <unistd.h>
/**
* get_converters_val - return the value of converters
* @n: integers
* Return: len of the integers
*/
unsigned long int get_converters_val(unsigned long int n)
{
char *buf;
long int len = 0, i = 0;
long int rev = 0;
if (n == 0)
{
write(1, "0", 1);
return (-1);
}
buf = malloc(sizeof(char) * (digit_counter(n) + 1));
if (!buf)
{
_puts("Memory allocation failed\n");
return (-1);
}
while (n > 0)
{
buf[i++] = (n % 10) + '0';
n /= 10;
}
for (rev = i - 1; rev >= 0; rev--)
{
write(STDOUT_FILENO, &buf[rev], 1);
}
len = i;
free(buf);
return (len);
}
/**
* create_special_heap - creates a memory in the heap
* according to their sign value
* @num: number of bytes to create the memory heap
* @sign: determines the space for sign symbol
* Return: pointer to the newly created memory int heap
*/
char *create_special_heap(int num, bool sign)
{
char *s;
s = malloc(sizeof(char) * (num + ((sign) ? 1 : 0) + 1));
if (!s)
{
_puts("Memory allocation failed");
return (NULL);
}
return (s);
}
/**
* digit_long_counter - counts the number of digits present in
* a long int
* @num: digits passed
* Return: number of digits present
*/
unsigned long int digit_long_counter(unsigned long int num)
{
unsigned long int temp = 0, count = 0;
temp = num;
count = (temp == 0) ? 1 : 0;
while (temp != 0)
{
temp /= 10;
count++;
}
return (count);
}
/**
* _puts - output the strings to the stdio
* @s: pointer to a string of chars
* Return: void
*/
void _puts(char *s)
{
int i = 0;
for (; s[i] != '\0'; i++)
{
write(STDOUT_FILENO, &s[i], 1);
}
write(STDOUT_FILENO, "\n", 1);
}
/**
* _strlen - get the length of a string
* @s: pointer to a string of chars
* Return: length of the string
*/
int _strlen(char *s)
{
int len = 0;
while (s[len] && s)
len++;
return (len);
}