-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstringc.c
More file actions
50 lines (45 loc) · 695 Bytes
/
stringc.c
File metadata and controls
50 lines (45 loc) · 695 Bytes
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
#include <stdlib.h>
#include <string.h>
char *strncpy(char *d, char *s, long n)
{
int len = strlen(s);
if (len > n)
len = n;
memcpy(d, s, len);
memset(d + len, 0, n - len);
return d;
}
char *strcat(char *d, char *s)
{
strcpy(d + strlen(d), s);
return d;
}
char *strstr(char *s, char *r)
{
int len = strlen(r);
if (!len)
return s;
while (s) {
if (!memcmp(s, r, len))
return s;
s = strchr(s + 1, *r);
}
return NULL;
}
char *strdup(const char *s)
{
size_t n = strlen(s) + 1;
char *res = malloc(n);
if (res)
memcpy(res, s, n);
return res;
}
char *strpbrk(char *s, char *r)
{
while (*s) {
if (strchr(r, (unsigned char) *s))
return s;
s++;
}
return NULL;
}