-
Notifications
You must be signed in to change notification settings - Fork 0
/
aux_functs.c
144 lines (137 loc) · 2 KB
/
aux_functs.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
133
134
135
136
137
138
139
140
141
142
143
144
#include "holberton.h"
/**
* str_concat - concatenates two strings.
* @s1: string 1
* @s2: string 2
* Return: pointer to str.
*/
char *str_concat(char *s1, char *s2)
{
char *str;
int a, b, size1, size2;
if (s1 == NULL)
{
s1 = "";
}
if (s2 == NULL)
{
s2 = "";
}
for (size1 = 0; *(s1 + size1) != '\0'; size1++)
{
}
for (size2 = 0; *(s2 + size2) != '\0'; size2++)
{
}
str = (char *)malloc(sizeof(char) * (size1 + size2) + 1);
if (str == NULL)
{
return (NULL);
}
for (a = 0; a < size1; a++)
{
*(str + a) = *(s1 + a);
}
for (b = 0; b <= (size2); b++, a++)
{
*(str + a) = *(s2 + b);
}
return (str);
}
/**
* _strcmp - Compares two strings
* @s1: String 1
* @s2: String 2
*
* Return: Always 0 (Success)
*/
int _strcmp(char s1[], char s2[])
{
int i = 0;
int flag = 0;
while (flag == 0)
{
if (s1[i] > s2[i])
{
flag = 1;
}
else if (s1[i] < s2[i])
{
flag = -1;
}
if (s1[i] == '\0')
{
break;
}
i++;
}
return (flag);
}
/**
* _strcpy - Copies one string into another
* @dest: String to be copied into
* @src: String to be copied
*
* Return: Copied string
*/
char *_strcpy(char *dest, char *src)
{
int i, n;
for (n = 0; *(src + n) != '\0'; n++)
{
}
if (n != 0)
{
for (i = 0; i <= n; i++)
{
dest[i] = src[i];
}
}
return (dest);
}
/**
* _realloc - reallocate memory
* @ptr: Pointer
* @old_size: Old size
* @new_size: New size to be allocated
*
* Return: pointer
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
if (old_size == new_size)
{
return (ptr);
}
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (new_size < old_size)
{
free(ptr);
}
free(ptr);
ptr = malloc(new_size);
return (ptr);
}
/**
* _strchr - check the code for Holberton School students.
* @s: char pointer to be determined.
* @c: char to compare.
* Return: pointer or null.
*/
char *_strchr(char *s, char c)
{
while (*s != c && *s != '\0')
{
s++;
}
if (*s == c)
{
return (s);
}
else
return (0);
}