|
3 | 3 | * strncmp, which operate on at most the first n characters of their argument |
4 | 4 | * strings. For example, strncpy(s, t, n) copies at most n characters of t to |
5 | 5 | * s. Full descriptions are in Appendix B. |
| 6 | + * |
6 | 7 | * By Faisal Saadatmand |
7 | 8 | */ |
8 | 9 |
|
|
12 | 13 | #define MAXCHAR 32 |
13 | 14 |
|
14 | 15 | /* functions */ |
15 | | -char *strnCpy(char *, char *, int); |
16 | | -char *strnCat(char *, char *, int); |
| 16 | +char *strnCpy(char *, char *, const size_t); |
| 17 | +char *strnCat(char *, char *, const size_t); |
| 18 | +int strnCmp(const char *, const char *, const size_t); |
17 | 19 |
|
18 | 20 | /* strnCpy: copy at most n characters of string t to s. Returns s. Pad with |
19 | 21 | * '\0's if t has fewer than n characters */ |
20 | | -char *strnCpy(char *s, char *t, int n) |
| 22 | +char *strnCpy(char *s, char *t, const size_t n) |
21 | 23 | { |
22 | | - int nChar; /* number of characters copied */ |
23 | | - int t_len; /* length of source string */ |
| 24 | + size_t i; |
24 | 25 |
|
25 | | - t_len = strlen(t); |
26 | | - nChar = 0; |
| 26 | + for (i = 0; i < n; ++i) |
| 27 | + if (!(*s++ = *t++)) /* copy the character */ |
| 28 | + *s = '\0'; /* pad with '\0's, if t's length is < n */ |
| 29 | + *s = '\0'; /* add the terminating character */ |
27 | 30 |
|
28 | | - if (n <= 0) /* invalid inputs */ |
29 | | - return t; |
30 | | - |
31 | | - while (nChar < n) { /* copy exactly n characters */ |
32 | | - *s++ = *t++; |
33 | | - ++nChar; |
34 | | - } |
35 | | - *s = '\0'; /* terminate with null character */ |
36 | | - |
37 | | - if (t_len < n) |
38 | | - while (nChar != n) { /* pad t with '\0' */ |
39 | | - *s++ = '\0'; |
40 | | - ++nChar; |
41 | | - } |
42 | 31 | return s - n; |
43 | | - |
44 | 32 | } |
45 | 33 |
|
46 | 34 | /* strnCat: concatenate at most n characters of string t to string s, terminate |
47 | | - * s with '\0'; return s*/ |
48 | | -char *strnCat(char *s, char *t, int n) |
| 35 | + * s with '\0'; return s */ |
| 36 | +char *strnCat(char *s, char *t, const size_t n) |
49 | 37 | { |
50 | | - int s_len; /* length of s */ |
51 | | - int nChar = 0; |
52 | | - |
53 | | - s_len = strlen(s); |
54 | | - s += s_len; /* find the end of s */ |
55 | | - |
56 | | - while (nChar < n) { |
57 | | - *s++ = *t++; |
58 | | - ++nChar; |
59 | | - } |
| 38 | + size_t i, s_len, t_len, lim; |
60 | 39 |
|
| 40 | + s += (s_len = strlen(s)); /* advance pointer */ |
| 41 | + lim = (n > (t_len = strlen(t))) ? t_len : n; /* scale down n */ |
| 42 | + for (i = 0; i < lim && (*s++ = *t++); ++i) |
| 43 | + ; |
61 | 44 | *s = '\0'; |
62 | | - |
63 | | - return s - s_len - nChar; |
| 45 | + return s - s_len - i; |
64 | 46 | } |
65 | 47 |
|
66 | 48 | /* strnCmp: compare at most n characters of string s to string t; return < 0 |
67 | 49 | * if s < t, 0 if s == t, or > 0 if s > t. */ |
68 | | -int strnCmp(char *s, char *t, int n) |
| 50 | +int strnCmp(const char *s, const char *t, const size_t n) |
69 | 51 | { |
70 | | - while (*s == *t) { |
71 | | - ++s; |
72 | | - ++t; |
73 | | - --n; |
74 | | - if (n == 0) |
| 52 | + size_t i; |
| 53 | + |
| 54 | + for (i = 1; i < n && *s == *t; ++s, ++t, ++i) |
| 55 | + if (*s == '\0') |
75 | 56 | return 0; |
76 | | - } |
77 | 57 | return *s - *t; |
78 | 58 | } |
79 | 59 |
|
80 | 60 | int main(void) |
81 | 61 | { |
82 | | - char dest1[MAXCHAR]; |
83 | | - char dest2[MAXCHAR] = { "It's a " }; |
84 | | - char source[MAXCHAR] = { "copy me" }; |
85 | | - char string1[MAXCHAR] = { "compare me" }; |
86 | | - char string2[MAXCHAR] = { "compare me with other strings" }; |
| 62 | + char dest[MAXCHAR]; |
| 63 | + |
| 64 | + printf("%s\n", strnCpy(dest, "copy me", 4)); |
| 65 | + printf("%s\n", strnCat(dest, "concatenate", 4)); |
| 66 | + printf("%i\n", strnCmp ("samee", "same", 4)); |
| 67 | + printf("%i\n", strnCmp ("samee", "same", 5)); |
87 | 68 |
|
88 | | - printf("%s\n", strnCpy(dest1, source, 4)); |
89 | | - printf("%s\n", strnCat (dest2, source, 4)); |
90 | | - printf("%i\n", strnCmp (string1, string2, 4)); |
91 | 69 | return 0; |
92 | 70 | } |
0 commit comments