-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.c
More file actions
171 lines (139 loc) · 2.64 KB
/
strings.c
File metadata and controls
171 lines (139 loc) · 2.64 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "strings.h"
#include <stdio.h>
/**
* _strcpy - makes a copy of a string
* @dest: address of buffer to copy string to
* @src: address of string to be copied
*
* Return: the pointer to dest. Otherwise NULL
*/
char *_strcpy(char *dest, const char *src)
{
unsigned int i;
if (src == NULL || dest == NULL)
return (NULL);
for (i = 0; src[i] != '\0'; i++)
dest[i] = src[i];
dest[i] = '\0';
return (dest);
}
/**
* _strlen - finds the length of a given string
* @str: given string
*
* Return: length of str
*/
unsigned int _strlen(const char *str)
{
unsigned int length = 0;
if (str == NULL)
return (0);
while (*str)
{
length++;
str++;
}
return (length);
}
/**
* _strcmp - compares 2 strings
* @s1: first string
* @s2: second string
*
* Description: works exactly like strcmp from <string.h>
*
* Return: -ve integer, 0 or +ve integer if s1 is less than,
* equal to or greater than s2
*/
int _strcmp(const char *s1, const char *s2)
{
int diff = 0;
while (diff == 0)
{
diff = *s1 - *s2;
/* break if one of the strings is empty */
if (*s1 == '\0' || *s2 == '\0')
break;
s1++;
s2++;
}
return (diff);
}
/**
* _strdup - creates a copy of a given string
* @str: string to copy
*
* Return: new string identical to str. Otherwise NULL
*/
char *_strdup(const char *str)
{
unsigned int length;
char *result;
if (str == NULL)
return (NULL);
/* allocate space for new string */
length = _strlen(str);
result = malloc(sizeof(char) * (length + 1));
if (result == NULL)
return (NULL);
/* copy contents of str into new string */
return (_strcpy(result, str));
}
/**
* _strtok - tokenizes a given string using a given delimiter
* @str: string
* @delim: delimiter
*
* Return: pointer to next token. Otherwise NULL
*/
char *_strtok(char *str, const char *delim)
{
char c;
int found_char = 0;
char *start;
static char *current;
start = (str ? str : current);
if (str)
current = str;
if (delim == NULL || *current == '\0')
return (NULL);
c = delim[0];
while (*current == c) /* ignore separators at beginning */
{
start++;
current++;
}
while (*current)
{
if (*current == c && found_char) /* found a delimiter */
{
*current = '\0';
current++;
break;
}
found_char = 1;
current++;
}
while (*current && *current == c) /* ignore separators at end */
current++;
return (start);
}
/**
* is_in_str - checks whether given character is in a given string
* @str: string
* @c: character
*
* Return: 1 if c is in str. 0 Otherwise
*/
int is_in_str(const char *str, char c)
{
if (str == NULL)
return (0);
while (*str)
{
if (*str == c)
return (1);
str++;
}
return (0);
}