-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_5.c
90 lines (70 loc) · 1.41 KB
/
ex5_5.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
char *my_strncpy(char *s, char *ct, int n)
{
int offset, pad;
offset = pad = 0;
while (offset < n) {
if (*(ct + offset) == '\0') {
pad = 1;
break;
}
*(s + offset) = *(ct + offset);
offset++;
}
s[offset] = '\0';
if (pad) {
while (offset++ < n) {
*(s + offset) = '\0';
}
}
return s;
}
char *my_strncat(char *s, char *ct, int n)
{
int offset = 0;
while (*s++ != '\0')
;
s--;
// above code is ugly should probably use strlen but
// did it for didatic purposes
while (offset < n) {
if (*(ct + offset) == '\0') {
break;
}
*(s + offset) = *(ct + offset);
offset++;
}
s[offset] = '\0';
return s;
}
int my_strncmp(char *cs, char *ct, int n)
{
int offset = 0;
while (offset < n) {
if (*(cs + offset) != *(ct + offset))
return (*(cs + offset) - *(ct + offset));
offset++;
}
return 0;
}
int main()
{
char s[100] = "";
char t[100] = "hey are you gonna copy this?";
char at[100] = "hey";
char a[5] = "abc";
char b[5] = "b";
char c[5] = "c";
char d[5] = "a";
my_strncpy(s, t, 15);
printf("%s\n", s);
my_strncpy(s, at, 15);
printf("%s\n", s);
my_strncat(t, at, 6);
printf("%s\n", t);
printf("%d\n", my_strncmp(b, c, 2));
printf("%d\n", my_strncmp(c, b, 2));
printf("%d\n", my_strncmp(a, d, 1));
printf("%d\n", my_strncmp(a, d, 5));
return 0;
}