-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_string_handlers.c
131 lines (114 loc) · 2.21 KB
/
basic_string_handlers.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
#include "shell.h"
/**
* _strcmp - compare two strings
* @s1: string 1
* @s2: string 2
*
* Return: 0 if @s1 and @s2 are equal, a negative value if @s1 is
* less than @s2, a positive value if @s1 is greater than @s2
*/
int _strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == '\0')
{
return (0); /* Both strings are equal */
}
s1++;
s2++;
}
return (*s1 - *s2);
}
/**
* _strcat - concatenate two strings
* @dest: destination string buffer
* @src: source string buffer
*
* Description: The _strcat() function appends the @src string to the @dest
* string, overwriting the terminating null byte ('\\0') at the end of dest,
* and then adds a terminating null byte.
*
* Return: a pointer to the resulting string dest
*/
char *_strcat(char *dest, const char *src)
{
int len, i;
len = _strlen(dest);
for (i = 0; src[i] != '\0'; i++)
{
dest[len++] = src[i];
}
dest[len] = '\0';
return (dest);
}
/**
* _strcpy - copy a string
* @dest: destination buffer
* @src: source buffer
*
* Description: copies the string pointed to by @src, including the terminating
* null byte ('\\0'), to the buffer pointed to by @dest. The destination string
* @dest must be large enough to receive the copy.
*
* Return: a pointer to the @dest string
*/
char *_strcpy(char *dest, const char *src)
{
size_t i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
/**
* _strlen - returns the length of a string
* @s: string
*
* Return: length of string
*/
size_t _strlen(const char *s)
{
int i = 0;
if (s == NULL)
{
return (0);
}
while (s[i] != '\0')
{
i++;
}
return (i);
}
/**
* get_operator - returns the && or || logical operator in a string if found
* @str: the string to search
*
* Return: the logical operator if found, else NULL
*/
char *get_operator(char *str)
{
char *operator = NULL;
int i;
if (str == NULL)
return (NULL);
/* Check for the "&&" operator */
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == '&' && str[i + 1] == '&')
{
operator = "&&";
break;
}
/* Check for the "||" operator */
else if (str[i] == '|' && str[i + 1] == '|')
{
operator = "||";
break;
}
}
return (operator);
}