forked from MatiasAcostaDiaz/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_funcs2.c
More file actions
83 lines (77 loc) · 1.28 KB
/
string_funcs2.c
File metadata and controls
83 lines (77 loc) · 1.28 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
#include "holberton.h"
/**
* _print_hexa - print an hexa in cap
* @args: the number passed
* Return: Nothing.
*/
void _print_hexa(unsigned int args)
{
int i = 0, length = 0, res = 0, j = 0;
unsigned int n = args;
char *string;
if (n == 0)
{
_putchar('0');
return;
}
if (n <= 16)
_putchar('0');
length = hexa_length(n);
string = malloc((length * sizeof(char)) + 1);
if (string == NULL)
return;
i = length - 1;
while (n > 0)
{
if (n <= 16)
{
if (n < 10)
string[i] = n + 48;
else
string[i] = hexa_cap(n);
break;
}
res = n % 16;
if (res >= 10)
string[i] = hexa_cap(res);
else
string[i] = res + 48;
n /= 16;
i--;
}
for (j = 0; j < length; j++)
_putchar(string[j]);
free(string);
}
/**
* print_string_ascii - print an string
* @args: argument passed
* Return: the count of characters printed
*/
int print_string_ascii(va_list args)
{
int i = 0, count = 0;
char *string;
string = va_arg(args, char*);
if (string == NULL)
return (-1);
if (string[0] == '\0')
return (0);
while (string[i] != '\0')
{
if ((string[i] > 0 && string[i] <= 32) || string[i] >= 127)
{
_putchar('\\');
_putchar('x');
_print_hexa(string[i]);
count += 4;
}
else
{
_putchar(string[i]);
count++;
}
i++;
}
return (count);
}