forked from kei-en/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_functions.c
113 lines (94 loc) · 2.12 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
#include "shell.h"
/**
* _strlen - Returns the length of a string.
* @s: A pointer to the characters string.
*
* Return: The length of the character string.
*/
int _strlen(const char *s)
{
int length = 0;
if (!s)
return (length);
for (length = 0; s[length]; length++)
;
return (length);
}
/**
* _strcpy - Copies the string pointed to by src, including the
* terminating null byte, to the buffer pointed by des.
* @dest: Pointer to the destination of copied string.
* @src: Pointer to the src of the source string.
*
* Return: Pointer to dest.
*/
char *_strcpy(char *dest, const char *src)
{
size_t o;
for (o = 0; src[o] != '\0'; o++)
dest[o] = src[o];
dest[o] = '\0';
return (dest);
}
/**
* _strcat - Concantenates two strings.
* @dest: Pointer to destination string.
* @src: Pointer to source string.
*
* Return: Pointer to destination string.
*/
char *_strcat(char *dest, const char *src)
{
char *destTemp;
const char *srcTemp;
destTemp = dest;
srcTemp = src;
while (*destTemp != '\0')
destTemp++;
while (*srcTemp != '\0')
*destTemp++ = *srcTemp++;
*destTemp = '\0';
return (dest);
}
/**
* _strncat - Concantenates two strings where n number
* of bytes are copied from source.
* @dest: Pointer to destination string.
* @src: Pointer to source string.
* @n: n bytes to copy from src.
*
* Return: Pointer to destination string.
*/
char *_strncat(char *dest, const char *src, size_t n)
{
size_t dest_len = _strlen(dest);
size_t o;
for (o = 0; o < n && src[o] != '\0'; o++)
dest[dest_len + o] = src[o];
dest[dest_len + o] = '\0';
return (dest);
}
/**
* _strdup - returns a pointer to a newly allocated space in memory,
* which contains a copy of the string given as a parameter
* @str: string to be copied
*
* Return: pointer to duplicated string (success), NULL (fail)
*/
char *_strdup(char *str)
{
char *cpy, *temp;
int length = 0;
if (str == NULL)
return (NULL);
for (; str[length]; length++)
;
cpy = malloc(sizeof(char) * (length + 1));
temp = cpy;
if (cpy == NULL)
return (NULL);
while (*str)
*temp++ = *str++;
*temp = '\0';
return (cpy);
}