-
Notifications
You must be signed in to change notification settings - Fork 0
/
strnx.c
62 lines (47 loc) · 1.39 KB
/
strnx.c
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
/* Exercise 5-5. Write versions of the library functions strncpy, strncat, and
* strncmp, which operate on at most the first n characters of their argument
* strings. For example, strncpy(s,t,n) copies at most n characters of t to s.
* Full descriptions are in Appendix B. */
#include "stdio.h"
#include "string.h"
void strncpy_(char *s, char *t, size_t n);
void strncat_(char *s, char *t, size_t n);
int strncmp_(char *s, char *t, size_t n);
int main(void) {
char s[] = "Brand New Day";
char t[] = "Luise";
printf("before strncat:\ns: %s\nt: %s\n", s, t);
strncat_(s, t, 4);
printf("after strncat t to s:\n%s\n", s);
putchar('\n');
printf("before strncpy:\ns: %s\nt: %s\n", s, t);
strncpy_(s, t, 8);
printf("after strncpy t to s:\n%s\n", s);
putchar('\n');
strcpy(s, "Good");
strcpy(t, "Goot");
printf("for comparison:\ns: %s\nt: %s\n", s, t);
printf("%d\n", strncmp_(s, t, 3));
}
void strncpy_(char *s, char *t, size_t n) {
char *tp = t;
while ((*s++ = *t++) && t - tp < n)
;
if (t - tp == n)
*s = '\0';
}
void strncat_(char *s, char *t, size_t n) {
// go to the end of s
while (*s++)
;
s--;
strncpy_(s, t, n);
}
int strncmp_(char *s, char *t, size_t n) {
char *tp = t;
for (; *s == *t && *s && *t && t - tp + 1 < n; s++, t++)
;
if (!*s || !*t || t - tp == n && *--s == *--t)
return 0;
return *s - *t;
}