-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstring.c
More file actions
51 lines (42 loc) · 1.07 KB
/
string.c
File metadata and controls
51 lines (42 loc) · 1.07 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
#include "string.h"
/*
Returns an integral value indicating the relationship between the strings:
return value indicates
<0 the first character that does not match has a lower value in ptr1 than in ptr2
0 the contents of both strings are equal
>0 the first character that does not match has a greater value in ptr1 than in ptr2
*/
int strcmp(const char * str1, const char * str2) {
while (*str1 != 0 && *str2 != 0) {
int diff = *str1 - *str2;
if (diff != 0) {
return diff;
}
str1++;
str2++;
}
// One of these points to 0
return *str1 - *str2;
}
int strncmp (const char * str1, const char * str2, uint32_t num) {
while (*str1 != 0 && *str2 != 0 && num > 1) {
int diff = *str1 - *str2;
if (diff != 0) {
return diff;
}
str1++;
str2++;
num--;
}
return *str1 - *str2;
}
void* memcpy (void* dest, const void* src, uint32_t count) {
char* dest_byte = (char*) dest;
char* src_byte = (char*) src;
for (uint32_t i = 0; i < count; i++) {
*dest_byte = *src_byte;
dest_byte++;
src_byte++;
}
return dest;
}