-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
103 lines (86 loc) · 1.73 KB
/
string.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
#include "shell.h"
/**
* _strlen - determines the string length
* @str: the string whoes length is to be determined
* Return: the length
*/
int _strlen(const char *str)
{
int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
/**
* _strcmp - compare two strings
* @str1: the first string to be conpared
* @str2: the second string to be compared
* Return: the compared string
*/
int _strcmp(const char *str1, const char *str2)
{
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0')
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (str1[i] - str2[i]);
}
/**
* _strcspn - get length of a prefix substring
* @str: the string
* @charset: the character set
* Return: returns the length
*/
size_t _strcspn(const char *str, const char *charset)
{
size_t len = 0;
const char *pointer = str, *charPointer = charset;
while (*pointer != '\0')
{
while (*charPointer != '\0')
{
if (*pointer == *charPointer)
return (len);
charPointer++;
}
pointer++;
len++;
}
return (len);
}
/**
* _strdup - duplicates the string
* @str: the string to me duplicated
* Return: the new string
*/
char *_strdup(char *str)
{
size_t length = _strlen(str) + 1;
char *new_str = malloc(length);
if (new_str == NULL)
return (NULL);
_memcpy(new_str, str, length);
return (new_str);
}
/**
* _memcpy - copy memory area
* @dest: the destination string
* @src: the source string
* @n: number of bytes to be copied
* Return: returns a pointer to dest
*/
void *_memcpy(void *dest, const void *src, size_t n)
{
unsigned char *dest_ptr = (unsigned char *)dest;
const unsigned char *src_ptr = (const unsigned char *)src;
size_t i = 0;
while (i < n)
{
dest_ptr[i] = src_ptr[i];
i++;
}
return (dest);
}