Skip to content

Commit 7860683

Browse files
committed
revise answer
1 parent 240cb25 commit 7860683

File tree

1 file changed

+29
-51
lines changed

1 file changed

+29
-51
lines changed

chapter05/5-5.c

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* strncmp, which operate on at most the first n characters of their argument
44
* strings. For example, strncpy(s, t, n) copies at most n characters of t to
55
* s. Full descriptions are in Appendix B.
6+
*
67
* By Faisal Saadatmand
78
*/
89

@@ -12,81 +13,58 @@
1213
#define MAXCHAR 32
1314

1415
/* 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);
1719

1820
/* strnCpy: copy at most n characters of string t to s. Returns s. Pad with
1921
* '\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)
2123
{
22-
int nChar; /* number of characters copied */
23-
int t_len; /* length of source string */
24+
size_t i;
2425

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 */
2730

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-
}
4231
return s - n;
43-
4432
}
4533

4634
/* 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)
4937
{
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;
6039

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+
;
6144
*s = '\0';
62-
63-
return s - s_len - nChar;
45+
return s - s_len - i;
6446
}
6547

6648
/* strnCmp: compare at most n characters of string s to string t; return < 0
6749
* 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)
6951
{
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')
7556
return 0;
76-
}
7757
return *s - *t;
7858
}
7959

8060
int main(void)
8161
{
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));
8768

88-
printf("%s\n", strnCpy(dest1, source, 4));
89-
printf("%s\n", strnCat (dest2, source, 4));
90-
printf("%i\n", strnCmp (string1, string2, 4));
9169
return 0;
9270
}

0 commit comments

Comments
 (0)