-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path5-5.c
70 lines (57 loc) · 1.8 KB
/
5-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
/*
* 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.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#include <string.h>
#define MAXCHAR 32
/* functions */
char *strnCpy(char *, char *, const size_t);
char *strnCat(char *, char *, const size_t);
int strnCmp(const char *, const char *, const size_t);
/* strnCpy: copy at most n characters of string t to s. Returns s. Pad with
* '\0's if t has fewer than n characters */
char *strnCpy(char *s, char *t, const size_t n)
{
size_t i;
for (i = 0; i < n; ++i)
if (!(*s++ = *t++)) /* copy the character */
*s = '\0'; /* pad with '\0's, if t's length is < n */
*s = '\0'; /* add the terminating character */
return s - n;
}
/* strnCat: concatenate at most n characters of string t to string s, terminate
* s with '\0'; return s */
char *strnCat(char *s, char *t, const size_t n)
{
size_t i, s_len, t_len, lim;
s += (s_len = strlen(s)); /* advance pointer */
lim = (n > (t_len = strlen(t))) ? t_len : n; /* scale down n */
for (i = 0; i < lim && (*s++ = *t++); ++i)
;
*s = '\0';
return s - s_len - i;
}
/* strnCmp: compare at most n characters of string s to string t; return < 0
* if s < t, 0 if s == t, or > 0 if s > t. */
int strnCmp(const char *s, const char *t, const size_t n)
{
size_t i;
for (i = 1; i < n && *s == *t; ++s, ++t, ++i)
if (*s == '\0')
return 0;
return *s - *t;
}
int main(void)
{
char dest[MAXCHAR];
printf("%s\n", strnCpy(dest, "copy me", 4));
printf("%s\n", strnCat(dest, "concatenate", 4));
printf("%i\n", strnCmp ("samee", "same", 4));
printf("%i\n", strnCmp ("samee", "same", 5));
return 0;
}