-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar_and_str.c
93 lines (82 loc) · 1.54 KB
/
char_and_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "main.h"
/**
*_putchar - prtins a single a character
*@c: the character to be printed
*Return: returns 1
*/
int _putchar(int c)
{
return (write(1, &c, 1));
}
/**
*putstr - prints a string and non printable hex values
*@s: the string to be printed
*@all: weather to print non printables or not
*Return: returns the size of the string
*/
int putstr(char *s, int all)
{
int i = 0;
int counter = 0;
if (!s)
s = "(null)";
while (s[i])
{
if (((s[i] > 0 && s[i] < 32) || s[i] >= 127) && all)
{
counter += _putchar('\\');
counter += _putchar('x');
if (s[i] < 16)
counter += _putchar('0');
putnbr_hex((unsigned int)s[i], 1, &counter);
}
else
counter += _putchar(s[i]);
i++;
}
return (counter);
}
/**
*rev_str - prints the reverse string
*@str: the string in hand
*Return: the number of chars in string
*/
int rev_str(char *str)
{
int i = 0;
int counter = 0;
if (!str)
return (putstr("(null)", 0));
while (str[i])
i++;
i--;
while (i >= 0)
{
counter += _putchar(str[i]);
i--;
}
return (counter);
}
/**
*rot13 - prints the rotated string
*@str: the string in hand
*Return: number of printed characters
*/
int rot13(char *str)
{
int i = 0;
int counter = 0;
if (!str)
return (putstr("(null)", 0));
while (str[i])
{
if (str[i] >= 'a' && str[i] <= 'z')
counter += _putchar((((str[i] - 'a') + 13) % 26) + 'a');
else if (str[i] >= 'A' && str[i] <= 'Z')
counter += _putchar((((str[i] - 'A') + 13) % 26) + 'A');
else
counter += _putchar(str[i]);
i++;
}
return (counter);
}