-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
39 lines (33 loc) · 845 Bytes
/
string.c
File metadata and controls
39 lines (33 loc) · 845 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
// string.c
#include "string.h"
int strlen(const char* str) {
int len = 0;
while (str[len]) len++;
return len;
}
void strcpy(char* dest, const char* src) {
int i = 0;
while ((dest[i] = src[i]) != '\0') i++;
}
int strcmp(const char* str1, const char* str2) {
int i = 0;
while (str1[i] && str2[i]) {
if (str1[i] != str2[i]) return str1[i] - str2[i];
i++;
}
return str1[i] - str2[i];
}
void strcat(char* dest, const char* src) {
int dest_len = strlen(dest);
int i = 0;
while ((dest[dest_len + i] = src[i]) != '\0') i++;
}
void memcpy(void* dest, const void* src, int n) {
char* d = dest;
const char* s = src;
for (int i = 0; i < n; i++) d[i] = s[i];
}
void memset(void* dest, int val, int n) {
char* d = dest;
for (int i = 0; i < n; i++) d[i] = (char)val;
}