-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_helper.c
More file actions
105 lines (84 loc) · 1.49 KB
/
string_helper.c
File metadata and controls
105 lines (84 loc) · 1.49 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
#include "shell.h"
/**
* _strdup - duplicate a string
* @str: string
* Return: If str is NULL or if malloc() fails - NULL
*/
char *_strdup(char *str)
{
char *array;
int length;
if (str == NULL)
return (NULL);
length = _strlen(str);
array = malloc(1 + (sizeof(char) * length));
if (array == NULL)
return (NULL);
array[length] = '\0';
while (length--)
array[length] = str[length];
return (array);
}
/**
* _strlen - string length
* @str: string pointer
* Return: string length
*/
int _strlen(char *str)
{
int len = 0;
while (*str != '\0')
{
len++;
str++;
}
return (len);
}
/**
* _strcpy - copy a string
* @destination: destination
* @source: source
* Return: destination string pointer
*/
char *_strcpy(char *destination, char *source)
{
int n;
for (n = 0; source[n] != '\0'; n++)
{
destination[n] = source[n];
}
destination[n] = '\0';
return (destination);
}
/**
* _strcmp - compare two strings
* @str1: 1st string
* @str2: 2nd string
* Return: integer <, = or > than zero
*/
int _strcmp(char *str1, char *str2)
{
while (*str1 && *str2 && *str1 == *str2)
{
str1++;
str2++;
}
return ((int)*str1 - (int)*str2);
}
/**
* _strcat - concat 2 string
* @destination: destination
* @source: source
* Return: destination result
*/
char *_strcat(char *destination, char *source)
{
int n, length;
length = _strlen(destination);
for (n = 0; source[n] != '\0'; n++)
{
destination[n + length] = source[n];
}
destination[n + length] = '\0';
return (destination);
}