-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhelper_1.c
121 lines (92 loc) · 1.91 KB
/
helper_1.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
#include "shell.h"
/**
*_strcmp - compare two strings
*@first: first string to be compared
*@second: second string to be compared
*
* Return: difference of the two strings
*/
int _strcmp(char *first, char *second)
{
int i = 0;
while (first[i] != '\0')
{
if (first[i] != second[i])
break;
i++;
}
return (first[i] - second[i]);
}
/**
*_strcat - concatenates two strings
*@destination: string to be concatenated to
*@source: string to concatenate
*
* Return: address of the new string
*/
char *_strcat(char *destination, char *source)
{
char *new_string = NULL;
int len_dest = _strlen(destination);
int len_source = _strlen(source);
new_string = malloc(sizeof(*new_string) * (len_dest + len_source + 1));
_strcpy(destination, new_string);
_strcpy(source, new_string + len_dest);
new_string[len_dest + len_source] = '\0';
return (new_string);
}
/**
*_strspn - gets the length of a prefix substring
*@str1: string to be searched
*@str2: string to be used
*
*Return: number of bytes in the initial segment of 5 which are part of accept
*/
int _strspn(char *str1, char *str2)
{
int i = 0;
int match = 0;
while (str1[i] != '\0')
{
if (_strchr(str2, str1[i]) == NULL)
break;
match++;
i++;
}
return (match);
}
/**
*_strcspn - computes segment of str1 which consists of characters not in str2
*@str1: string to be searched
*@str2: string to be used
*
*Return: index at which a char in str1 exists in str2
*/
int _strcspn(char *str1, char *str2)
{
int len = 0, i;
for (i = 0; str1[i] != '\0'; i++)
{
if (_strchr(str2, str1[i]) != NULL)
break;
len++;
}
return (len);
}
/**
*_strchr - locates a char in a string
*@s: string to be searched
*@c: char to be checked
*
*Return: pointer to the first occurence of c in s
*/
char *_strchr(char *s, char c)
{
int i = 0;
for (; s[i] != c && s[i] != '\0'; i++)
;
if (s[i] == c)
return (s + i);
else
return (NULL);
}