-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_strings.c
134 lines (112 loc) · 2.08 KB
/
print_strings.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
#include <stddef.h>
#include "main.h"
/**
* _puts - prints a string
*
* @str: the string to print
* Return: number of characters printed (the length of the string)
*/
int _puts(const char *str)
{
int i;
if (str == NULL)
{
str = "(null)";
}
for (i = 0; *(str + i) != '\0'; i++)
{
_putchar(*(str + i));
}
return (i);
}
/**
* handle_s - prints a string
*
* @s: the string to be printed
* Return: length of the string printed
*/
int handle_s(va_list s)
{
char *str = va_arg(s, char *);
if (str == NULL)
{
return (_puts("(null)"));
}
return (_puts(str));
}
/**
* handle_ascii - prints a string, including non printable characters.
* The non printable characters are printed this way: \x,
* followed by the ASCII code value in hexadecimal
* (upper case - always 2 characters).
*
* @ascii: the string to be printed.
* Return: the number of characters printed.
*/
int handle_ascii(va_list ascii)
{
unsigned int i;
char tmp[100];
int count = 0;
char *str = va_arg(ascii, char *);
if (str == NULL)
{
return (_puts("(null)"));
}
for (i = 0; *(str + i); i++)
{
if (*(str + i) < 32 || *(str + i) >= 127)
{
_putchar('\\');
_putchar('x');
count += 2;
convert_bases(((unsigned int) *(str + i)), tmp, 16, 1);
if (_strlen(tmp) < 2)
count += _putchar('0');
count += _puts(tmp);
}
else
{
_putchar(*(str + i));
count++;
}
}
return (count);
}
/**
* handle_r - prints a string in reverse.
*
* @r: the string to be printed.
* Return: the number of characters printed.
*/
int handle_r(va_list r)
{
char *str = va_arg(r, char *);
int i, count = 0;
if (str == NULL)
{
return (_puts("(null)"));
}
for (i = _strlen(str) - 1; i >= 0; i--)
{
count += _putchar(*(str + i));
}
return (count);
}
/**
* handle_rot13 - prints a string encrypted by the ROT-13 cypher.
*
* @l: the string to be printed.
* Return: the number of characters printed.
*/
int handle_rot13(va_list l)
{
char tmp[1024];
char *str = va_arg(l, char *);
if (str == NULL)
{
return (_puts("(null)"));
}
_strcpy(tmp, str);
return (_puts(rot13(tmp)));
}