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
|
#include <stdio.h>
/* The C Programming Language: 2nd Edition
*
* Exercise 5-5: Write versions of the library functions strncpy, strncat, and
* strncmp, which operate on at most the first n charecetr of their argument
* strings. For example, strncpy(s,t,n) copies at most n character of t to s.
* Full descriptions are in Appendix B.
*/
char *strn_cpy(char *, const char *, size_t);
char *strn_cat(char *, const char *, size_t);
int strn_cmp(const char *, const char *, size_t);
int main() {
/* This is to fill the array up beyond where I'll be filling it so I can
* test. */
char foo[50] = "Meepineeeeeessssss";
char *bar = "Merpy";
printf("strn_cpy: %s\n", strn_cpy(foo, bar, 9));
/* Test to see that the last element of the range is indeed a \0, and
* some other part further is something else. */
if (foo[8] == '\0' && foo[14] != '\0') {
printf("strn_cpy works as expected.\n");
}
bar = " hai";
printf("strn_cat: %s\n", strn_cat(foo, bar, 9));
// bar is 4 characters long. Let's test comparisons.
printf("strn_cmp: %d\n", strn_cmp(bar, "hai lits", 10)); // -4
printf("strn_cmp: %d\n", strn_cmp(bar, "no", 10)); // 2
printf("strn_cmp: %d\n", strn_cmp(bar, "derp", 10)); // 0
return 0;
}
char *strn_cpy(char *s, const char *t, size_t n) {
char *start = s;
while (n > 0 && *t != '\0') {
*s++ = *t++;
n--;
}
while (n > 0) {
*s++ = '\0';
n--;
}
return start;
}
char *strn_cat(char *s, const char *t, size_t n) {
char *start = s;
while (*s != '\0') {
s++;
}
while (*t != '\0' && n > 0) {
*s++ = *t++;
n--;
}
*s = '\0';
return start;
}
int strn_cmp(const char *s, const char *t, size_t n) {
int slen, tlen;
slen = tlen = 0;
while (n > 0) {
if (*s != '\0') {
s++;
slen++;
}
if (*t != '\0') {
t++;
tlen++;
}
n--;
}
return slen - tlen;
}
|