-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_2.c
More file actions
216 lines (173 loc) · 3.51 KB
/
str_2.c
File metadata and controls
216 lines (173 loc) · 3.51 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "shell.h"
char *strdup_func(const char *s);
char *strncat_alt(char *dest, char *src, int n);
size_t strspn_alt(char *s, char *accept);
int strncmp_alt(const char *s1, const char *s2, int n);
char *strncpy_alt(char *destination, char *source, int n);
int atoi_alt(char *s);
/**
* strdup_func - Entry point
* Description - 'a function that duplicates a string including allocating
* memory dynamically'
* @s: string to be duplicated
*
* Return: a pointer to the duplicated string
*/
char *strdup_func(const char *s)
{
int m = 0;
char *copy;
int len = 0;
if (s == NULL)
{
return (NULL);
}
while (s[len] != '\0')
{
len++;
}
copy = malloc((len + 1) * sizeof(char));
if (copy == NULL)
{
/* if memory allocation fails */
return (NULL);
}
for (m = 0; m < len; m++)
{
copy[m] = s[m];
}
copy[m] = '\0';
return (copy);
}
/**
* strncat_alt - Entry point
* Description - 'a function used to concantenate a specified number of chars'
* @src: a pointer to the source string
* @dest: a pointer to the destination string
* @n: the maximum number of chars to be concantenated
*
* Return: pointer to dest
*/
char *strncat_alt(char *dest, char *src, int n)
{
int dest_len = strlen_alt(dest);
int m;
for (m = 0; m < n && src[m] != '\0'; m++)
{
dest[dest_len + m] = src[m];
}
dest[dest_len + m] = '\0';
return (dest);
}
/**
* strspn_alt - Entry point
* Description - ' a function that gets the length of a prefix substring'
* @s: string to be checked
* @accept: the substring to be checked
*
* Return: the number of bytes within s that only belongs to accept
*/
size_t strspn_alt(char *s, char *accept)
{
size_t h, m;
for (h = 0; s[h] != '\0'; h++)
{
for (m = 0; accept[m] != '\0'; m++)
{
if (s[h] == accept[m])
{
break;
}
}
if (accept[m] == '\0')
{
break;
}
}
return (h);
}
/**
* strncmp_alt - Entry point
* Description - ' a function that compares two strings according to a specif-
* ' ied number of bytes or characters'
* @s1: pointer to a string
* @s2: pointer to another string
* @n: specified or maximum number of bytes to be used for comparing
*
* Return: if value is less than 0, then s1 is shorter or less than s2
* if value is 0 then s1 and s2 are the same or have a match
* if value is greater than 0, then s2 is longer than s1
*/
int strncmp_alt(const char *s1, const char *s2, int n)
{
int m = 0;
/* an iterator that keeps count of bytes or characters*/
while (s1[m] && s2[m] && m < n)
{
if (s1[m] != s2[m])
{
return (s1[m] - s2[m]);
}
m++;
}
if (m == n)
{
return (0);
}
if (s1[m] && !s2[m])
{
return (1);
}
else if (!s1[m] && s2[m])
{
return (-1);
}
return (0);
}
/**
* strncpy_alt - Entry point
* Description -'a function that copies a string'
* @destination: destination char string type
* @source: initial char string type
* @n: maximum number of bytes to be used
* Return: pointer to destination
*/
char *strncpy_alt(char *destination, char *source, int n)
{
int cob;
for (cob = 0; cob < n && source[cob] != '\0'; cob++)
{
destination[cob] = source[cob];
}
for (; cob < n; cob++)
{
destination[cob] = '\0';
}
return (destination);
}
/**
* atoi_alt - Entry point
* Description -'funtion that converts a chracter string to an integer'
* @s: the string to be converted
* Return: converted interger value
*/
int atoi_alt(char *s)
{
int res_value = 0;
int sn = 1;
int h = 0;
while (s[h] == ' ' || s[h] == '\t' || s[h] == '\n')
{
h++;
}
if (s[h] == '+' || s[h] == '-')
{
sn = (s[h++] == '-') ? -1 : 1;
}
while (s[h] >= '0' && s[h] <= '9')
{
res_value = res_value * 10 + (s[h] - '0');
h++;
}
return (res_value * sn);
}