-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_functions.c
More file actions
107 lines (82 loc) · 1.84 KB
/
string_functions.c
File metadata and controls
107 lines (82 loc) · 1.84 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
#include "holberton.h"
#include <stdlib.h>
/**
* _strdup - string duplicate
* Description: Duplicates a string
* @str: Source string
* Return: Pointer to newly created string
*/
char *_strdup(char *str)
{
int index, size;
char *dst;
if (str == NULL)
return (NULL);
size = 0;
while (*(str + size))
size++;
dst = malloc(size + 1);
if (dst == NULL)
return (NULL);
for (index = 0; index < size; index++)
*(dst + index) = *(str + index);
*(dst + index) = '\0';
return (dst);
}
/**
* _calloc - character allocate
* Description: Allocates memory for an array and sets each element to zero
* @nmemb: Number of elements in the array
* @size: Size of each element
* @c: character with which to fill each byte
* Return: Pointer to allocated memory, or NULL if unsuccessful.
*/
void *_calloc(unsigned int nmemb, unsigned int size, char c)
{
char *pointer;
unsigned int index;
if (nmemb == 0 || size == 0)
return (NULL);
pointer = malloc(nmemb * size);
if (pointer == NULL)
return (NULL);
for (index = 0; index < nmemb * size; index++)
*(pointer + index) = c;
return ((void *)pointer);
}
/**
* _strlen - Returns lenght of a string
* @s: Incoming string
* Description: Counts the number of charaters in a string, returns that number
* Return: Length of the string
*/
int _strlen(char *s)
{
int strlen = 0;
while (*(s + strlen))
strlen++;
return (strlen);
}
/**
* _cstrdup - string duplicate
* Description: Duplicates a string
* @str: Source string
* Return: Pointer to newly created string
*/
char *_cstrdup(const char *str)
{
int index, size;
char *dst;
if (str == NULL)
return (NULL);
size = 0;
while (*(str + size))
size++;
dst = malloc(size + 1);
if (dst == NULL)
return (NULL);
for (index = 0; index < size; index++)
*(dst + index) = *(str + index);
*(dst + index) = '\0';
return (dst);
}