-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_func2.c
More file actions
110 lines (88 loc) · 1.69 KB
/
str_func2.c
File metadata and controls
110 lines (88 loc) · 1.69 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
#include "main.h"
/**
* _strlen - returns the length of a string
* @s: string to evaluate
*
* Return: finlal iterated value as len of string
*/
int _strlen(const char *s)
{
int i;
for (i = 0; s[i] != '\0'; i++)
;
return (i);
}
/**
* *_strcpy - function that copies the string pointed to by src,
* including the terminating null byte (\0), to the buffer pointed to by dest.
* @dest: pointer to the buffer in which we copy the string
* @src: string to be copied
* Return: the pointer to dest
*/
char *_strcpy(char *dest, char *src)
{
int a;
for (a = 0; src[a] != '\0'; a++)
dest[a] = src[a];
dest[a] = '\0';
return (dest);
}
/**
* _strncpy - copies a string
* @dest: destination string
* @src: source string
* @n: number of bytes to copy
* Return: pointer to the resulting string
*/
char *_strncpy(char *dest, char *src, size_t n)
{
size_t i;
for (i = 0; src[i] != '\0' && i < n; i++)
{
dest[i] = src[i];
}
for (; i < n; i++)
dest[i] = '\0';
return (dest);
}
/**
* _strcat - concatenate two strings
* @dest: char pointer the dest of the copied str
* @src: const char pointer the source of str
* Return: the dest
*/
char *_strcat(char *dest, const char *src)
{
int i;
int j;
for (i = 0; dest[i] != '\0'; i++)
;
for (j = 0; src[j] != '\0'; j++)
{
dest[i] = src[j];
i++;
}
dest[i] = '\0';
return (dest);
}
/**
* _strdup - duplicates a str in the heap memory.
* @str: pointer to char array
* Return: duplicated str
*/
char *_strdup(char *str)
{
char *dup;
size_t len;
if (!str)
return (NULL);
len = _strlen(str);
dup = malloc((len + 1));
if (!dup)
{
perror("Error allocating memory");
return (NULL);
}
_strcpy(dup, str);
return (dup);
}