Repository navigation
Expand file tree
/
Copy pathstring_funcs.c
More file actions
144 lines (124 loc) · 2.01 KB
/
Copy pathstring_funcs.c
File metadata and controls
144 lines (124 loc) · 2.01 KB
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
#include "main.h"
/**
* _strncmp - compares two strings
* @s1: string one
* @s2: string two
* @n: specified number of bytes
* Return: 0 if the strings match for the specified number of bytes
* -1, otherwise
*/
int _strncmp(const char *s1, const char *s2, int n)
{
int i;
if (n == 0)
{
return (-1);
}
i = 0;
while (s1[i] != '\0' || s2[i] != '\0')
{
if (i == n)
{
return (0);
}
if (s1[i] != s2[i])
{
return (-1);
}
i = i + 1;
}
if (s1[i] == '\0' && s2[i] == '\0')
{
return (0);
}
return (-1);
}
/**
* _strlen - is a function that returns the length of a string.
* @s: is the character being passed.
* Return: len is the string length.
*/
int _strlen(const char *s)
{
int len;
len = 0;
while (s != NULL && s[len] != '\0')
{
len = len + 1;
}
return (len);
}
/**
* _strdup - duplicates an array and stores it in a memory allocation
* @str: pointer to the string array
* Return: The duplicated array stored in new pointer
*/
char *_strdup(char *str)
{
char *dupe;
int strLen, array;
if (str == NULL)
{
return (NULL);
}
strLen = _strlen(str);
dupe = malloc(strLen * sizeof(*dupe) + 1);
if (dupe == NULL)
{
return (NULL);
}
array = 0;
while (str[array] != '\0')
{
dupe[array] = str[array];
array = array + 1;
}
dupe[array] = '\0';
return (dupe);
}
/**
* *_strcat - concatenates two strings
* @src: string to appended
* @dest: string to append to
* Return: pointer to dest
*/
char *_strcat(char *dest, char *src)
{
int i, j;
i = 0;
while (dest[i] != '\0')
{
i = i + 1;
}
j = 0;
while (src[j] != '\0')
{
dest[i] = src[j];
j = j + 1;
i = i + 1;
}
dest[i] = '\0';
return (dest);
}
/**
* *_strcpy - copies a string to a buffer
* @dest: points to buffer location
* @src: points to string location
* Return: pointer to dest
*/
char *_strcpy(char *dest, char *src)
{
int i, length;
length = 0;
while (src[length] != '\0')
{
length = length + 1;
}
i = 0;
while (i <= length)
{
dest[i] = src[i];
i = i + 1;
}
return (dest);
}