-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_strops.c
More file actions
148 lines (135 loc) · 2.54 KB
/
_strops.c
File metadata and controls
148 lines (135 loc) · 2.54 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "holberton.h"
/**
*_strcmp - compares two strings
*@s1: string 1
*@s2: string 2
*
*Return: negative difference if s1 is less than s2, positive
*difference if s1 is greater than s2, or 0 if equal
*/
int _strcmp(char *s1, char *s2)
{
while ((*s1 == *s2) && *s1 != '\0' && *s2 != '\0')
{
s1++;
s2++;
}
if ((*s1 == '\0') && (*s2 == '\0'))
return (0);
else if (*s1 > *s2)
return ((int)(*s1 - *s2));
else
return ((int)(*s1 - *s2));
}
/**
*_strdup - returns pointer to allocated space containing copy of string
*@str: string that will be copied into the space
*
*Return: pointer to allocated space
*/
char *_strdup(char *str)
{
int i = 0;
int length = 0;
char *string;
if (str == NULL)
return (NULL);
while (*(str + i) != '\0')
{
length++;
i++;
}
i = 0;
string = malloc(sizeof(char) * length + 1);
if (string == NULL)
return (NULL);
while (*(str + i) != '\0')
{
*(string + i) = *(str + i);
i++;
}
*(string + i) = '\0';
return (string);
}
/**
*_strlen - calculates the length of a string
*@buf: buffer to count length of strings
*
*Return: the length of the string
*/
int _strlen(char *buf)
{
int i = 0;
if (buf == NULL)
return (-1);
while (buf[i] != '\0')
{
i++;
}
return (i);
}
/**
*str_concat - concatenates two strings
*@s1: first string
*@s2: second string to concatenate to first
*
*Return: pointer to concatenated string
*/
char *str_concat(char *s1, char *s2)
{
int lengths1 = 0;
int lengths2 = 0;
char *concatenate;
if (s1 == NULL)
{
s1 = malloc(sizeof(char));
if (s1 == NULL)
return (NULL);
*s1 = '\0';
}
if (s2 == NULL)
{
s2 = malloc(sizeof(char));
if (s2 == NULL)
return (NULL);
*s2 = '\0';
}
lengths1 = _strlen(s1);
lengths2 = _strlen(s2);
concatenate = malloc(sizeof(char) * (lengths1 + lengths2 + 1));
if (concatenate == NULL)
{
free(s1);
free(s2);
return (NULL);
}
return (_concat(concatenate, s1, s2));
}
/**
*_concat - concatenates two strings
*@concatenate: memory space to concatenate the strings
*@s1: string 1
*@s2: string 2
*
*Return: pointer to concatenated space
*/
char *_concat(char *concatenate, char *s1, char *s2)
{
int concatcounter = 0;
int stringcounter = 0;
while (*(s1 + stringcounter) != '\0')
{
*(concatenate + concatcounter) = *(s1 + stringcounter);
concatcounter++;
stringcounter++;
}
stringcounter = 0;
while (*(s2 + stringcounter) != '\0')
{
*(concatenate + concatcounter) = *(s2 + stringcounter);
concatcounter++;
stringcounter++;
}
*(concatenate + concatcounter) = '\0';
return (concatenate);
}