-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstr_funcs.c
More file actions
124 lines (108 loc) · 1.57 KB
/
str_funcs.c
File metadata and controls
124 lines (108 loc) · 1.57 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
#include "shell.h"
/**
* _strdup - pointer to newly alloced mem
* @str: string input
* Return: string
*/
char *_strdup(char *str)
{
int x = 0;
char *ptr = NULL;
if (str == NULL)
{
return (NULL);
}
ptr = (char *)malloc(_strlen(str) + 1);
if (ptr == NULL)
{
return (NULL);
}
while (str[x])
{
ptr[x] = str[x];
x++;
}
ptr[x] = '\0';
return (ptr);
}
/**
* _strlen - gets string length
* @s: pointer input
* Return: int
*/
int _strlen(char *s)
{
int i = 0;
while (*(s + i) != '\0')
{
i++;
}
return (i);
}
/**
* *_strcpy - copies string
* @dest: pointer input
* @src: pointer input
* Return: char
*/
char *_strcpy(char *dest, char *src)
{
int x = 0;
for (; *(src + x) != '\0'; x++)
{
*(dest + x) = *(src + x);
}
*(dest + x) = *(src + x);
return (dest);
}
/**
* _realloc - reallocates mem
* @ptr: ptr
* @old_size: un int
* @new_size: un int
* Return: ptr
*/
char *_realloc(char *ptr, unsigned int old_size, unsigned int new_size)
{
char *nptr;
int x = 0;
if (new_size == 0 && !ptr)
{
return (NULL);
}
if (new_size == old_size)
return (ptr);
if (!ptr)
{
ptr = malloc(new_size);
if (!ptr)
return (NULL);
return (ptr);
}
nptr = malloc(new_size);
if (!nptr)
return (NULL);
for (; ptr[x]; x++)
nptr[x] = ptr[x];
nptr[x] = '\0';
free(ptr);
return (nptr);
}
/**
* _strcmp - compares two strings
* @s1: str input
* @s2: str input
* Return: int
*/
int _strcmp(char *s1, char *s2)
{
int x = 0;
for (; s1[x] != '\0'; x++)
{
if (s1[x] != s2[x])
{
return (s1[x] - s2[x]);
}
}
return (s1[x] - s2[x]);
}