forked from AlfredPianist/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_string.c
executable file
·122 lines (105 loc) · 2.22 KB
/
_string.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
#include "holberton.h"
/**
* str_concat - Concatenates a string to the buffer.
* @orig: The string to be concatenated.
* @buffer: The buffer.
* @buff_len: The length of buffer
* @buffer_i: The index of the buffer.
*/
void str_concat(char *orig, char *buffer, int *buffer_i,
unsigned int *buff_len)
{
int index;
index = 0;
while (orig[index] != '\0')
check_buffer(buffer, buffer_i, buff_len, orig[index++]);
}
/**
* str_len - Calculates the length of a string.
* @str: The string to be calculated.
*
* Return: The length of the string.
*/
unsigned int str_len(char *str)
{
unsigned int len;
len = 0;
while (str[len++] != '\0')
;
return (len);
}
/**
* str_cpy - Copies a string to another string.
* @orig: The original string.
* @dest: The destination string.
*
* Return: pointer to destination string
*/
char *str_cpy(char *orig, char *dest)
{
int index;
index = 0;
while (orig[index] != '\0')
{
dest[index] = orig[index];
index++;
}
dest[index] = '\0';
return (dest);
}
/**
* str_cpy_h - Copies a string to another string, if the string has
* non printable characters, then convert them to hex: \xXX.
* @orig: The original string.
* @dest: The destination string.
*
* Return: pointer to destination string
*/
char *str_cpy_h(char *orig, char *dest)
{
unsigned int index_o, index_d, index_h;
char *hex;
index_o = index_d = index_h = 0;
hex = NULL;
while (orig[index_o] != '\0')
{
if (orig[index_o] < 32 || orig[index_o] >= 127)
{
hex = _uitoa_b_o_h(orig[index_o], 16, 1);
dest[index_d++] = '\\';
dest[index_d++] = 'x';
if (hex[index_h + 1] == '\0')
dest[index_d++] = '0';
while (hex[index_h] != '\0')
dest[index_d++] = hex[index_h++];
free(hex);
index_o++;
index_h = 0;
}
else
dest[index_d++] = orig[index_o++];
}
dest[index_d] = '\0';
return (dest);
}
/**
* str_cmp - Compares two strings for a number of characters.
* @str1: The first string.
* @str2: The second string.
*
* Return: 1 if equal, 0 if different.
*/
int str_cmp(const char *str1, char *str2)
{
short index, len;
len = index = 0;
while (str2[len++] != '\0')
;
while (index < len - 1)
{
if (str1[index] != str2[index])
return (0);
index++;
}
return (index);
}