-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-5.c
More file actions
54 lines (45 loc) · 733 Bytes
/
5-5.c
File metadata and controls
54 lines (45 loc) · 733 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
51
52
53
54
#include <u.h>
#include <libc.h>
void strcpyn(char *, char *, int n);
void strcatn(char *, char *, int n);
int strcmpn(char *, char *, int n);
void
main(void)
{
char source[128] = { 'd', 'a', 't', 'a' };
char dest[128] = { 'd', 'a', 't', 'a' };
int n;
print("KR 5-5\n");
n = 3;
strcmpn(source,dest, n);
print("n is: %d\n", n);
}
int
strcmpn(char *s, char *t, int n)
{
int i;
for ( ; *s == *t; s++, t++) {
print("s is: %c\nt is: %c\n", *s, *t);
if (!(*s))
return 0;
}
return *s - *t;
}
void
strcatn(char *s, char *t, int n)
{
int c;
while (*s)
s++;
c = 0;
while (*t && c++ < n)
*s++ = *t++;
}
void
strcpyn(char *s, char *t, int n)
{
int c;
c = 0;
while ((*t++ = *s++) && c++ < (n - 1))
;
}