-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_str.c
166 lines (141 loc) · 2.61 KB
/
helper_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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "shell.h"
/**
* str_length - returns the length of a string.
*
* @string: pointer to string.
*
* Return: length of string.
*/
int str_length(char *string)
{
int length = 0;
if (string == NULL)
return (0);
while (string[length++] != '\0')
{
}
return (--length);
}
/**
* str_duplicate - duplicates an string
*
* @string: String to be copied
*
* Return: pointer to the array
*/
char *str_duplicate(char *string)
{
char *result;
int length;
int i;
if (string == NULL)
return (NULL);
length = str_length(string) + 1;
result = malloc(sizeof(char) * length);
if (result == NULL)
{
errno = ENOMEM;
perror("Error");
return (NULL);
}
for (i = 0; i < length ; i++)
{
result[i] = string[i];
}
return (result);
}
/**
* str_compare - Compare two strings
*
* @string1: String one, or the shorter
*
* @string2: String two, or the longer
*
* @number: number of characters
*
* Return: 1 if the strings are = 0 if different
*/
int str_compare(char *string1, char *string2, int number)
{
int iterator;
if (string1 == NULL && string2 == NULL)
return (1);
if (string1 == NULL || string2 == NULL)
return (0);
if (number == 0)
{
if (str_length(string1) != str_length(string2))
return (0);
for (iterator = 0; string1[iterator]; iterator++)
{
if (string1[iterator] != string2[iterator])
return (0);
}
return (1);
}
else
{
for (iterator = 0; iterator < number ; iterator++)
{
if (string1[iterator] != string2[iterator])
return (0);
}
return (1);
}
}
/**
* str_concat - concatenates two strings.
*
* @string1: String to be concatenated
*
* @string2: String to be concatenated
*
* Return: pointer to the array
*/
char *str_concat(char *string1, char *string2)
{
char *result;
int length1 = 0, length2 = 0;
if (string1 == NULL)
string1 = "";
length1 = str_length(string1);
if (string2 == NULL)
string2 = "";
length2 = str_length(string2);
result = malloc(sizeof(char) * (length1 + length2 + 1));
if (result == NULL)
{
errno = ENOMEM;
perror("Error");
return (NULL);
}
for (length1 = 0; string1[length1] != '\0'; length1++)
result[length1] = string1[length1];
free(string1);
for (length2 = 0; string2[length2] != '\0'; length2++)
{
result[length1] = string2[length2];
length1++;
}
result[length1] = '\0';
return (result);
}
/**
* str_reverse - reverses a string.
*
* @string: pointer to string.
*
* Return: void.
*/
void str_reverse(char *string)
{
int i = 0,
int length = str_length(string) - 1;
char hold;
while (i < length)
{
hold = string[i];
string[i++] = string[length];
string[length--] = hold;
}
}