-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilityFunc.c
116 lines (94 loc) · 1.85 KB
/
utilityFunc.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
#include "main.h"
/*================================================*/
/*STRING FUNCTIONS*/
/**
* _strlen - returns the length of a string
* @s: the string whose length to check
*
* Return: integer length of string
*/
int _strlen(char *s)
{
int i = 0;
if (!s)
return (0);
while (*s++)
i++;
return (i);
}
/**
* _strdup - duplicates a string
* @str: the string to duplicate
*
* Return: pointer to the duplicated string
*/
char *_strdup(const char *str)
{
int length = 0;
char *ret;
if (str == NULL)
return (NULL);
while (*str++)
length++;
ret = malloc(sizeof(char) * (length + 1));
if (!ret)
return (NULL);
for (length++; length--;)
ret[length] = *--str;
return (ret);
}
/**
* _strcmp - compares two strings
* @s1: string 1
* @s2: string 2
* Return: 0 if equal, negative if s1 is less than s2
* positive if s1 is less than s2
*/
int _strcmp(const char *s1, const char *s2)
{
int i;
for (i = 0; s1[i] != '\0' || s2[i] != '\0'; i++)
{
if (s1[i] != s2[i])
{
return (s1[i] - s2[i]);
}
}
return (0);
}
/**
* _strncmp - Custom implementation of strncmp.
* @s1: The first string.
* @s2: The second string.
* @n:The maximum number of characters to compare.
* Return: add descr
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
if (s1[i] != s2[i])
return ((int)(unsigned char)s1[i] - (int)(unsigned char)s2[i]);
if (s1[i] == '\0')
return (0);
}
return (0);
}
/**
* string_concatenate - Concatenates two strings.
* @destination: The destination buffer.
* @source: The source buffer.
*
* Return: Pointer to the destination buffer.
*/
char *string_concatenate(char *destination, char *source)
{
char *result = destination;
while (*destination)
destination++;
while (*source)
*destination++ = *source++;
*destination = *source;
return (result);
}