-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_7.c
47 lines (45 loc) · 850 Bytes
/
task_7.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
#include "holberton.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/**
* print_S - prints a string & allows to display values that aren't ASCII
* @S: string to print
* Description: if out ASCII range print as \\x
* Return: number of chars printed
*/
int print_S(va_list S)
{
unsigned int i; /* iterator */
int j = 0, k; /* print counters */
char c = 'A' - ':';/* ASCII fix */
char d[2]; /* digit ASCII array */
char *s = va_arg(S, char *);
if (s == NULL)
s = "(null)";
for (i = 0; s[i]; i++)
{
if (s[i] < 32 || s[i] > 126)
{
_putchar('\\');
_putchar('x');
j += 2;
d[0] = s[i] / 16;
d[1] = s[i] % 16;
for (k = 0; k < 2; k++)
{
if (d[k] >= 10)
_putchar('0' + c + d[k]);
else
_putchar('0' + d[k]);
}
j += k;
}
else
{
_putchar(s[i]);
j++;
}
}
return (j);
}