-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_fcts.c
190 lines (167 loc) · 3.66 KB
/
str_fcts.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "main.h"
char *_strchr(char *s, char c);
int _strspn(char *s, char *accept);
int _strcmp(char *s1, char *s2);
int _strncmp(const char *s1, const char *s2, size_t n);
int _strlen(const char *s);
char *_strcpy(char *dest, const char *src);
char *_strcat(char *dest, const char *src);
char *_strncat(char *dest, const char *src, size_t n);
/**
* _strchr - Locates a character in a string.
* @s: The string to be searched.
* @c: The character to be located.
*
* Return: If c is found - a pointer to the first occurence.
* If c is not found - NULL.
*/
char *_strchr(char *s, char c)
{
int index;
for (index = 0; s[index]; index++)
{
if (s[index] == c)
return (s + index);
}
return (NULL);
}
/**
* _strspn - Gets the length of a prefix substring.
* @s: The string to be searched.
* @accept: The prefix to be measured.
*
* Return: The number of bytes in s which
* consist only of bytes from accept.
*/
int _strspn(char *s, char *accept)
{
int bytes = 0;
int index;
while (*s)
{
for (index = 0; accept[index]; index++)
{
if (*s == accept[index])
{
bytes++;
break;
}
}
s++;
}
return (bytes);
}
/**
* _strcmp - Compares two strings.
* @s1: The first string to be compared.
* @s2: The second string to be compared.
*
* Return: Positive byte difference if s1 > s2
* 0 if s1 = s2
* Negative byte difference if s1 < s2
*/
int _strcmp(char *s1, char *s2)
{
while (*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s1 != *s2)
return (*s1 - *s2);
return (0);
}
/**
* _strncmp - Compare two strings.
* @s1: Pointer to a string.
* @s2: Pointer to a string.
* @n: The first n bytes of the strings to compare.
*
* Return: Less than 0 if s1 is shorter than s2.
* 0 if s1 and s2 match.
* Greater than 0 if s1 is longer than s2.
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
for (i = 0; s1[i] && s2[i] && i < n; i++)
{
if (s1[i] > s2[i])
return (s1[i] - s2[i]);
else if (s1[i] < s2[i])
return (s1[i] - s2[i]);
}
if (i == n)
return (0);
else
return (-15);
}
/**
* _strlen - Returns the length of a string.
* @s: A pointer to the characters string.
*
* Return: The length of the character string.
*/
int _strlen(const char *s)
{
int length = 0;
if (!s)
return (length);
for (length = 0; s[length]; length++)
;
return (length);
}
/**
* _strcpy - Copies the string pointed to by src, including the
* terminating null byte, to the buffer pointed by des.
* @dest: Pointer to the destination of copied string.
* @src: Pointer to the src of the source string.
*
* Return: Pointer to dest.
*/
char *_strcpy(char *dest, const char *src)
{
size_t i;
for (i = 0; src[i] != '\0'; i++)
dest[i] = src[i];
dest[i] = '\0';
return (dest);
}
/**
* _strcat - Concantenates two strings.
* @dest: Pointer to destination string.
* @src: Pointer to source string.
*
* Return: Pointer to destination string.
*/
char *_strcat(char *dest, const char *src)
{
char *destTemp;
const char *srcTemp;
destTemp = dest;
srcTemp = src;
while (*destTemp != '\0')
destTemp++;
while (*srcTemp != '\0')
*destTemp++ = *srcTemp++;
*destTemp = '\0';
return (dest);
}
/**
* _strncat - Concantenates two strings where n number
* of bytes are copied from source.
* @dest: Pointer to destination string.
* @src: Pointer to source string.
* @n: n bytes to copy from src.
*
* Return: Pointer to destination string.
*/
char *_strncat(char *dest, const char *src, size_t n)
{
size_t dest_len = _strlen(dest);
size_t i;
for (i = 0; i < n && src[i] != '\0'; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';
return (dest);
}