-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
154 lines (127 loc) · 2.22 KB
/
functions.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "holberton.h"
/**
* print_char - prints characters
*
* @arg_list: arguments list
*
* Return: number of characters printed
*/
int print_char(va_list arg_list)
{
int c = va_arg(arg_list, int);
_putchar(c);
return (1);
}
/**
* print_string - prints strings
*
* @arg_list: arguments list
*
* Return: number of characters printed
*/
int print_string(va_list arg_list)
{
char *string;
int print_count;
string = va_arg(arg_list, char *);
if (string == NULL)
{
string = "(null)";
}
for (print_count = 0; string[print_count] != '\0'; print_count++)
_putchar(string[print_count]);
return (print_count);
}
/**
* print_decimal - prints decimal numbers
*
* @arg_list: arguments list
*
* Return: number of characters printed
*/
int print_decimal(va_list arg_list)
{
long int d = va_arg(arg_list, int), mult = 1, dc, print_count = 0;
if (d < 0)
{
d = -d;
_putchar('-');
print_count++;
}
dc = d;
if (d < 10 && d >= 0)
{
_putchar(d + '0');
print_count++;
}
else
{
while (dc > 9)
{
mult *= 10;
dc /= 10;
}
while (mult > 0)
{
_putchar((d / mult) + '0');
print_count++;
d -= (d / mult) * mult;
mult /= 10;
}
}
return (print_count);
}
/**
* print_hex - prints hex numbers
*
* @arg_list: arguments list
*
* Return: number of characters printed
*/
int print_hex(va_list arg_list)
{
unsigned int num = va_arg(arg_list, unsigned int);
unsigned int num2;
int i, i2, copy, contame = 0;
char *numhex;
if (num == 0)
return (_putchar('0'));
for (num2 = num; num2 != 0; contame++)
{
num2 = num2 / 16;
}
numhex = malloc(contame);
for (i = 0; num != 0; i++)
{
copy = num % 16;
if (copy < 10)
numhex[i] = copy + '0';
else
numhex[i] = copy - 10 + 'a';
num = num / 16;
}
for (i2 = i - 1; i2 >= 0; i2--)
_putchar(numhex[i2]);
free(numhex);
return (contame);
}
/**
* inttostr - convert numbers to string
*
* @number: number to convert
*
* Return: the string containing the number
*/
char *inttostr(int number)
{
int i, nlen = intlen(number), rem;
char *str = malloc(sizeof(char) * nlen + 1);
for (i = 0; i < nlen; i++)
{
rem = number % 10;
number = number / 10;
str[nlen - (i + 1)] = rem + '0';
}
str[nlen] = '\0';
return (str);
}