-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
127 lines (101 loc) · 2.12 KB
/
string.c
File metadata and controls
127 lines (101 loc) · 2.12 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "shell.h"
/**
* _strlen - prints the length of a string
* @s: string
* Return: string length value
*/
unsigned int _strlen(const char *s)
{
int length = 0;
if (!s)
return (length);
while (s[length] != '\0')
length++;
return (length);
}
/**
* _strcpy - copies the string pointed to by src,
* including the terminating null byte (\0), to the buffer pointed to by dest
* @dest : destination buffer
* @src : source string
* Return: pointer to dest. If src is NULL, dest remains unchanged.
*/
char *_strcpy(char *dest, char *src)
{
char *end = src;
int i, len;
if (!src)
return (dest);
while (*end != '\0')
end++;
len = end - src;
for (i = 0; i <= len; i++)
dest[i] = src[i];
return (dest);
}
/**
* _strcat - concatenates two strings
* @dest: string to be appended to
* @src: string to append
* Return: concatenation of strings. If src is NULL, dest remains unchanged
*/
char *_strcat(char *dest, char *src)
{
int dest_end = 0;
int i = 0;
if (!src)
return (dest);
while (dest[dest_end] != '\0')
dest_end++;
while (src[i] != '\0')
{
dest[dest_end + i] = src[i];
i++;
}
dest[dest_end + i] = '\0';
return (dest);
}
/**
* _strncmp - compares the first n bytes of two strings
* @s1: first string
* @s2: second string
* @n: number of bytes to compare; if n = 0, the full lengths of the strings
* are compared until one terminates (with a null byte).
* Return: difference between first unequal ASCII char values
*/
int _strncmp(const char *s1, const char *s2, unsigned int n)
{
int i = 0;
int comp_all_flag = !n;
while (s1[i] != '\0' && s2[i] != '\0' && (--n || comp_all_flag))
{
if (s1[i] != s2[i])
break;
i++;
}
return (s1[i] - s2[i]);
}
/**
* _strdup - allocates memory and copies a string from input
* @str: string to copy
* Return: pointer to copied string, otherwise NULL
*/
char *_strdup(const char *str)
{
char *s;
unsigned int i;
unsigned int length = 0;
if (!str)
return (NULL);
while (str[length])
length++;
s = malloc(sizeof(char) * length + 1);
if (!s)
return (NULL);
else
{
for (i = 0; i <= length; i++)
s[i] = str[i];
}
return (s);
}