-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrn.c
134 lines (107 loc) · 2.44 KB
/
strn.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 "string.h"
/**
* _strnchr - get the index of the first matching character
* @str: string passed
* @chr: character passed
* @n: max number of characters to check
* Return: Index of the first occurence, or -1 chr is not found
*/
ssize_t _strnchr(const char *str, char chr, size_t n)
{
ssize_t index;
if (!str)
return (-1);
for (index = 0; n && str[index]; --n, ++index)
{
if (str[index] == chr)
return (index);
}
return (-1);
}
/**
* _strndup - duplicate the given string
* @str: the string to duplicate
* @n: the max number of bytes to copy
*
* Description: This function copies at most n bytes. If str is longer
* than n, only n bytes are copied, and a terminating null byte is added.
*
* Return: If str is NULL or if memory allocation fails, return NULL.
* Otherwise a return a pointer to the dynamically-allocated duplicate.
*/
char *_strndup(const char *str, size_t n)
{
char *dup;
size_t len = 0;
if (!str)
return (NULL);
while (n && str[len])
--n, ++len;
dup = malloc(sizeof(char) * (len + 1));
if (!dup)
return (NULL);
dup[len] = '\0';
while (len--)
dup[len] = str[len];
return (dup);
}
/**
* _strnlen - calculate the length of a string
* @str: the string to measure
* @n: the max number of characters to check
* Return: the lesser of n and the length of the string
*/
ssize_t _strnlen(const char *str, size_t n)
{
const char *pos = str;
if (!str)
return (-1);
while (n && *pos)
--n, ++pos;
return (pos - str);
}
/**
* _strncmp - compare two strings
* @s1: a string to compare
* @s2: the other string to compare
* @n: the max number of bytes to compare
* Return: 0 if s1 matches s2,
* otherwise an integer less than 0 if s1 is less than s2,
* otherwise an integer greater than 0 if s1 is greater than s2.
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
for (; n && *s1 && *s2; --n, ++s1, ++s2)
{
if (*s1 != *s2)
return (*s1 - *s2);
}
if (n)
{
if (*s1)
return (1);
if (*s2)
return (-1);
}
return (0);
}
/**
* _strncpy - copy the string
* @dest: destination
* @src: source
* @n: the max number of bytes to copy
*
* Description: This function copies at most n bytes from src to dest. A
* null byte will NOT be written if not found in the first n bytes
*
* Return: a pointer to dest
*/
char *_strncpy(char *dest, const char *src, size_t n)
{
char *pos = dest;
for ( ; n && *src; --n)
*pos++ = *src++;
if (n)
*pos = '\0';
return (dest);
}