-
Notifications
You must be signed in to change notification settings - Fork 0
/
string2.c
121 lines (102 loc) · 1.81 KB
/
string2.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
#include "shell.h"
/**
* _strspn - get length of a prefix substring
* @str: the string to be used
* @charset: the set of characters to match against
* Return: the function count
*/
size_t _strspn(const char *str, const char *charset)
{
size_t i = 0;
const char *ptr;
while (*str)
{
ptr = charset;
while (*ptr)
{
if (*str == *ptr)
{
i++;
break;
}
ptr++;
}
if (!*ptr)
break;
str++;
}
return (i);
}
/**
* _strchr - locate character in string
* @str: the string where the character is to be located
* @character: the character to be located
* Return: pointer to the matched character
*/
char *_strchr(const char *str, int character)
{
while (*str != '\0')
{
if (*str == character)
{
return ((char *)str);
}
str++;
}
return (NULL);
}
/**
* _strncmp - compare two strings
* @s1: the first string to be compared
* @s2: the second string to be compared
* @n: the first n bytes to be compared
* Return: return an interger if successful
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
size_t i = 0;
while (i < n)
{
if (s1[i] != s2[i])
return (s1[i] - s2[i]);
if (s1[i] == '\0' || s2[i] == '\0')
break;
i++;
}
return (0);
}
/**
* _strcpy - copy a string
* @to: the destination string
* @from: the source string
* Return: the destination string
*/
char *_strcpy(char *to, const char *from)
{
int i = 0;
while (from[i] != '\0')
{
to[i] = from[i];
i++;
}
to[i] = '\0';
return (to);
}
/**
* _strcat - concatenate two strings
* @to: the destination string
* @from: the source string
* Return: the destination string
*/
char *_strcat(char *to, const char *from)
{
int to_len = 0, i = 0;
while (to[to_len] != '\0')
to_len++;
for (; from[i] != '\0'; i++)
{
to[to_len + i] = from[i];
}
to[to_len + i] = '\0';
return (to);
}