-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path505.c
More file actions
37 lines (29 loc) · 716 Bytes
/
505.c
File metadata and controls
37 lines (29 loc) · 716 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
/*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.
*/
void strncpy(char * s, char * t, size_t n) {
int i;
int end = 0;
for (i = 0; i < n; i++) {
* s++ = * t++;
}
* s = '\0';
}
void strncat(char * s, char * t, size_t n) {
int i;
for (;* s; s++);
for (i = 0;
(i < n) && ( * s++ = * t++); i++);
* s = '\0';
}
int strncmp(char * s, char * t, size_t n) {
int i;
if (n == 0) return 0;
for (i = 1;
(i < n) && * s == * t; s++, t++, i++) {
if ( * s == '\0')
return 0;
}
return *s - * t;
}