-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_str.c
73 lines (63 loc) · 1.37 KB
/
print_str.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
#include "main.h"
/**
* print_string - Prints a null-terminated string to the standard output.
*
* @va: The argument list containing the string to print.
*
* Return: The length of the printed string.
*/
int print_string(va_list va)
{
/** Extract the string to print from the argument list*/
char *string = va_arg(va, char *);
/** Initialize a counter to track the number of characters printed*/
int counter = 0;
/** Handle the case when the string pointer is NULL*/
if (string == NULL)
{
string = "(null)";
}
/** Iterate through the string until the null terminator is encountered*/
while (*string != '\0')
{
_putchar(*string);
string++;
counter++;
}
/** Return the total length of the printed string*/
return (counter);
}
/**
* print_String - Prints a null-terminated string to the standard output.
* with special print for non printable char.
* @va: The argument list containing the string to print.
*
* Return: The length of the printed string.
*/
int print_String(va_list va)
{
char *string = va_arg(va, char *);
int counter = 0;
if (string == NULL)
{
string = "(null)";
}
while (*string != '\0')
{
if (*string < 32 || *string >= 127)
{
_putchar('\\');
_putchar('x');
_putchar('0');
counter += 2;
counter += _printf("%X", *string);
}
else
{
_putchar(*string);
}
string++;
counter++;
}
return (counter);
}