-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_func.c
More file actions
92 lines (78 loc) · 1.54 KB
/
str_func.c
File metadata and controls
92 lines (78 loc) · 1.54 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
#include "main.h"
/**
* _strcmp - Compares two strings.
* @s1: The first string to be compared.
* @s2: The second string to be compared.
*
* Return: Positive byte difference if s1 > s2
* 0 if s1 = s2
* Negative byte difference if s1 < s2
*/
int _strcmp(char *s1, char *s2)
{
while (*s1 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s1 != *s2)
return (*s1 - *s2);
return (0);
}
/**
* _strncmp - Compare two strings.
* @s1: Pointer to a string.
* @s2: Pointer to a string.
* @n: The first n bytes of the strings to compare.
*
* Return: Less than 0 if s1 is shorter than s2.
* 0 if s1 and s2 match.
* Greater than 0 if s1 is longer than s2.
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
int diff;
for (i = 0; s1[i] && s2[i] && i < n; i++)
{
diff = (unsigned char)s1[i] - (unsigned char)s2[i];
if (diff != 0)
return (diff);
}
if (i == n)
return (0);
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
/**
* _strchr - locates a character in a string,
* @s: string.
* @c: character.
* Return: the pointer to the first occurrence of the character c.
*/
char *_strchr(char *s, char c)
{
while (*s)
{
if (*s == c)
return (s);
s++;
}
return (*s == c ? s : NULL);
}
/**
* _strrchr - locates the last character in a string,
* @s: string.
* @c: character.
* Return: the pointer to the first occurrence of the character c.
*/
char *_strrchr(char *s, int c)
{
char *last_char = NULL;
while (*s)
{
if (*s == c)
last_char = s;
s++;
}
return (last_char);
}