-
Notifications
You must be signed in to change notification settings - Fork 1
/
_putspecial.c
59 lines (51 loc) · 866 Bytes
/
_putspecial.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
#include "main.h"
/**
* _putnospec - Prints a string to standard output
* @str: The string to be printed
*
* Return: Length of string printed - 1
*/
int _putnospec(char *str)
{
int i, len = 0;
if (!str)
return (len += _putstr("(null)"));
for (i = 0; str[i]; i++)
{
if (str[i] < 32 || str[i] >= 127)
{
len += _putchar(92);
len += _putchar('x');
len += _puthex2(str[i]);
continue;
}
len += _putchar(str[i]);
}
return (len);
}
/**
* _puthex2 - Prints an hex to standard output
* @num: The number to be printed
* Return: Length of string printed - 1
*/
int _puthex2(int num)
{
int val;
if (num < 10)
{
_putchar('0');
_putchar(num + '0');
return (2);
}
if (num < 16)
{
_putchar('0');
_putchar(num + 55);
return (2);
}
val = num % 16;
num = num / 16;
_puthex(num, 'X');
_puthex(val, 'X');
return (2);
}