Repository navigation
Expand file tree
/
Copy pathstring_functions.c
More file actions
120 lines (104 loc) · 1.71 KB
/
Copy pathstring_functions.c
File metadata and controls
120 lines (104 loc) · 1.71 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
#include "holberton.h"
/**
* _strlen - Calculates length of a string.
* @str: String that will be measured.
* Return: Returns length of string.
*/
int _strlen(char *str)
{
int i = 0;
for (i = 0; str[i] != '\0'; i++)
{
;
}
return (i);
}
/**
* _strcmp - Compares two strings.
* @str1: First string
* @str2: Second string.
* Return: Returns 0 if strings are different, returns 1 otherwise.
*/
int _strcmp(char *str1, char *str2)
{
int i = 0;
while (str1[i])
{
if (str1[i] != str2[i])
{
return (0);
}
i++;
}
return (1);
}
/**
*_strcat - Function that concatenates two strings.
*@dest: String to be added at the end of original.
*@src: Original string.
*Return: Returns pointer to dest.
*/
char *_strcat(char *dest, char *src)
{
int i = 0, j = 0;
while (dest[i] != '\0')
i++;
for (j = 0; src[j] != '\0'; j++)
{
dest[i] = src[j];
i++;
}
return (dest);
}
/**
* _strncmp - Compares two strings.
* @s1: First string
* @s2: Second string.
* @n: Number of characters to be compared.
* Return: Returns 0 if strings are different, returns 1 otherwise.
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
while (n && *s1 && (*s1 == *s2))
{
++s1;
++s2;
--n;
}
if (n == 0)
{
return (0);
}
else
{
return (*(unsigned char *)s1 - *(unsigned char *)s2);
}
}
/**
* _strdup - Duplicate a string.
* @str1: string to be duplicated.
* Return: Returns a pointer that points to the copy of str1.
*/
char *_strdup(char *str1)
{
int i = 0, j = 0;
char *str2 = NULL;
if (str1 == NULL)
{
return (NULL);
}
for (i = 0; str1[i] != '\0'; i++)
{
;
}
str2 = malloc(sizeof(char) * (i + 1));
if (str2 == NULL)
{
return (NULL);
}
for (j = 0; j <= i; j++)
{
str2[j] = str1[j];
}
return (str2);
}