-
Notifications
You must be signed in to change notification settings - Fork 2
/
str_tools.c
132 lines (117 loc) · 1.74 KB
/
str_tools.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "headers.h"
/**
* _strlen - Function that returns string lenght
* @s: pointer to string
* Return: Length
*/
int _strlen(char *s)
{
int l = 0;
while (*s != '\0')
{
l++;
s++;
}
return (l);
}
/**
* _strncmp - Function that returns string lenght
* @s1: pointer to string
* @s2: pointer to string
* @n: number of positions to compare
* Return: Compared string
*/
int _strncmp(char *s1, char *s2, int n)
{
int i = 0;
while (i < n)
{
if (s1[i] != s2[i])
return (-1);
else if (s1[i] == s2[i])
i++;
}
return (0);
}
/**
* str_concat - concatenate to strings
*@s1: string
*@s2: string
* Return: the string concat
*/
char *str_concat(char *s1, char *s2)
{
char *array;
int i = 0, l1 = 0, l2 = 0;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
while (s1[l1] != '\0')
{
l1++;
}
while (s2[l2] != '\0')
{
l2++;
}
array = malloc(sizeof(char) * (l1 + l2 + 1));
if (array == NULL)
return (NULL);
while (*s1)
{
array[i] = *s1;
i++;
s1++;
}
while (*s2)
{
array[i] = *s2;
i++;
s2++;
}
array[i] = '\0';
return (array);
}
/**
* *_strncpy - check the code for Holberton School students.
*@dest: char
*@src: char
*@n: int
* Return: Always 0.
*/
char *_strncpy(char *dest, char *src, int n)
{
int i;
for (i = 0; i < n && src[i] != 0; i++)
{
dest[i] = src[i];
}
for (; i < n; i++)
{
dest[i] = '\0';
}
return (dest);
}
/**
* _strdup - string duplicate with malloc
*@str: string
* Return: the string
*/
char *_strdup(char *str)
{
char *copy;
int i, largo = 0;
if (!str)
return (NULL);
while (str[largo] != '\0')
{
largo++;
}
copy = malloc(sizeof(char) * (largo + 1));
if (copy == NULL)
return (NULL);
for (i = 0; i <= largo; i++)
copy[i] = str[i];
return (copy);
}