-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_text.c
111 lines (88 loc) · 1.47 KB
/
utils_text.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
#include "text.h"
/**
* _strlen - Return the length of a string
*
* @msg: String to calculate length
*
* Return: Length of the string
**/
int _strlen(char *msg)
{
int i;
for (i = 0; msg[i] != '\0'; i++)
;
return (i);
}
/**
* _strcat - Concatenates two strings
*
* @dest: String concatenate
* @src: Source string
*
* Return: A string concatenate
*/
char *_strcat(char *dest, char *src)
{
int l_dest, i;
l_dest = _strlen(dest);
for (i = 0; src[i] != '\0'; i++, l_dest++)
dest[l_dest] = src[i];
dest[l_dest] = '\0';
return (dest);
}
/**
* _strcpy - Copy a string to another
*
* @dest: Destination copy
* @src: Source for copy
*
* Return: Return the value of dest
*/
char *_strcpy(char *dest, char *src)
{
int i;
for (i = 0; src[i] != '\0'; i++)
dest[i] = src[i];
dest[i] = '\0';
return (dest);
}
/**
* _strdup - Pointer to a newly allocated space in memory
*
* @str: String to copy
*
* Return: String copied
**/
char *_strdup(char *str)
{
int size, i;
char *dest;
if (str == NULL)
return (NULL);
size = _strlen(str) + 1;
dest = malloc(size * sizeof(char));
if (dest == NULL)
return (NULL);
for (i = 0; i < size; i++)
*(dest + i) = *(str + i);
/* (dest + i) = 0; */
return (dest);
}
/**
* _strcmp - Compare two strings
*
* @s1: String 1
* @s2: String 2
*
* Return: Integer
*/
int _strcmp(char *s1, char *s2)
{
int i;
for (i = 0; s1[i] != '\0'; i++)
{
if (s1[i] != s2[i])
return (s1[i] - s2[i]);
}
return (0);
}