forked from Hordunlarmy/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_functions.c
More file actions
124 lines (101 loc) · 1.86 KB
/
str_functions.c
File metadata and controls
124 lines (101 loc) · 1.86 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 "main.h"
/**
* _strlen - Entry point
* @s: string checked
* Return: the length of a string.
*/
int _strlen(const char *s)
{
int lent;
if (s == NULL)
return (-1);
lent = 0;
while (s[lent] != '\0')
lent++;
return (lent);
}
/**
* _strcmp - Entry point
* @s1: pointer variable 1
* @s2: pointer variable 2
* Return: result of two strings compared
*/
int _strcmp(const char *s1, const char *s2)
{
const char *p1 = s1;
const char *p2 = s2;
int i = 0;
if (p1 == NULL && p2 == NULL)
return (0);
else if (p1 == NULL)
return (-1);
else if (p2 == NULL)
return (1);
while (*(p1 + i) != '\0' && *(p2 + i) != '\0' && p1[i] == p2[i])
{
i++;
}
return (p1[i] - p2[i]);
}
/**
* _strchr - Entry point
* @s: pointer variable
* @c: character to be checked
* Return: Always 0 (Success)
*/
char *_strchr(const char *s, const char c)
{
const char *p = s;
const char *pp = &c;
int i;
for (i = 0; *(p + i) != '\0'; i++)
if (*(p + i) == *pp)
{
return ((char *)(s + i));
}
return (0);
}
/**
* _strdup - Entry point
* @str: string to duplicate
* Return: Always 0 (Success)
*/
char *_strdup(const char *str)
{
int i, len = 0;
char *r_value;
if (str == NULL)
return (NULL);
for (; str[len] != '\0'; len++)
;
r_value = malloc(sizeof(char) * (len + 1));
if (r_value == NULL)
return (NULL);
for (i = 0; i <= len; i++)
r_value[i] = str[i];
return (r_value);
}
/**
* _strncmp - Entry point
* @s1: 1st string to compare
* @s2: 2nd string to compare
* @n: Maximum number of characters to compare
* Return: Always 0 (Success)
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
unsigned char p1, p2;
size_t i;
if (s1 == NULL || s2 == NULL)
return (-1);
for (i = 0; i < n; i++)
{
p1 = (unsigned char)s1[i];
p2 = (unsigned char)s2[i];
if (p1 != p2)
return (p1 - p2);
if (p1 == '\0')
break;
}
return (0);
}