-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_functions.c
114 lines (94 loc) · 2.17 KB
/
string_functions.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
#include "shell.h"
/**
* str_len - checks the length of a string
* @string: pointer to string to check
*
* Return: int length of the string
*/
int str_len(char *string)
{
int len = 0;
while (string[len])
len++;
return (len);
}
/**
* str_dup - Returns a pointer to a newly-allocated space in memory
* containing a copy of the string given as parameter.
* @string: The string to be copied.
*
* Return: If str == NULL or insufficient memory is available - NULL.
* Otherwise - a pointer to the duplicated string.
*/
char *str_dup(char *string)
{
int i = 0;
int length = 0;
char *dup = NULL;
if (string == NULL)
return (NULL);
for (i = 0; *(string + i); i++)
length++;
dup = malloc(sizeof(char) * (length + 10));
if (dup == NULL)
return (NULL);
for (i = 0; *(string + i); i++)
*(dup + i) = *(string + i);
*(dup + length) = '\0';
return (dup);
}
/**
* *str_cpy - copies the string pointed to by src
* @dest: char type string
* @src: char type string
* Description: Copy the string pointed to by pointer `src` to
* the buffer pointed to by `dest`
* Return: Pointer to dest
*/
char *str_cpy(char *dest, char *src)
{
int i = -1;
do {
i++;
dest[i] = src[i];
} while (src[i] != '\0');
return (dest);
}
/**
* *str_cat - concatenates @src to @dest
* @src: the source string to append to @dest
* @dest: the destiation string to be concatenated upon
* Return: char
*/
char *str_cat(char *dest, char *src)
{
int index = 0;
int dest_len = 0;
while (dest[index++])
dest_len++;
for (index = 0; src[index]; index++)
dest[dest_len++] = src[index];
return (dest);
}
/**
* _strncmp - Compares at most the first n bytes of two strings.
* @str1: A pointer to the first string to be compared.
* @str2: A pointer to the second string to be compared.
* @n: number of bytes to compare
*
* Return: If str1 < str2, the negative difference of
* the first unmatched characters.
* If str1 == str2, 0.
* If str1 > str2, the positive difference of
* the first unmatched characters.
*/
int _strncmp(char *str1, char *str2, size_t n)
{
while ((*str1 && *str2) && (*str1 == *str2))
{
str1++;
str2++;
n--;
}
return (*str1 - *str2);
}