-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_funcs1.c
127 lines (112 loc) · 2.22 KB
/
utils_funcs1.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
#include "shell.h"
/**
* _strlen - returns the length of a string
* @s: the string whose length to check
*
* Return: integer length of string
*/
int _strlen(const char *s)
{
int i = 0;
if (!s)
return (0);
while (*s++)
i++;
return (i);
}
/**
* _strcmp - Compare two strings.
* @s1: The first string to compare.
* @s2: The second string to compare.
* Return: 0 if @s1 and @s2 are equal,
* a negative value if @s1 is less than @s2,
* or a positive value if @s1 is greater than @s2.
*/
int _strcmp(const char *s1, const char *s2)
{
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
return ((int) (*s1) - (*s2));
}
/**
* _strncmp - Compare two strings up to a specified length.
* @s1: First string to compare.
* @s2: Second string to compare.
* @n: Maximum number of characters to compare.
*
* Return: 0 if the strings are equal up to n characters, negative value
* if s1 is less than s2, or positive value if s1 is greater than s2.
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
unsigned char c1, c2;
while (n-- > 0)
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 != c2)
return (c1 - c2);
if (c1 == '\0')
break;
}
return (0);
}
/**
* _strstr - checks if needle starts with haystack
* @haystack: string to search
* @needle: the substring to find
*
* Return: address of next char of haystack or NULL
*/
char *_strstr(char *haystack, char *needle)
{
int i;
for (i = 0; haystack[i] != '\0'; i++)
{
if (haystack[i] == needle[0])
{
int j;
for (j = 0; needle[j] != '\0'; j++)
{
if (haystack[i + j] != needle[j])
{
break;
}
}
if (needle[j] == '\0')
{
return (&haystack[i]);
}
}
}
return (NULL);
}
/**
* _strchr - a function that locates a character in a string
*
* @s: pointer to our string array input
* @c: character to locate from input array
*
* Return: first occurence of charatcer or null if not found
*/
char *_strchr(char *s, char c)
{
while (*s != '\0')
{
if (*s == c)
return (s);
s++;
}
/**
* if c is '\0', you should return
* the pointer to the '\0' of the
* string s
*/
if (*s == c)
return (s);
/*return null if not found*/
return (NULL);
}