Skip to content

Commit 0e7e874

Browse files
committed
Completed exercise 5-05
Completed exercise 5-05 - versions of strncpy(), strncmp(), and strncat()
1 parent 5b71105 commit 0e7e874

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ SUBDIRS = src
55

66
all:
77
@for i in $(SUBDIRS); do \
8-
echo "make all in $$i..."; \
98
cd $$i; \
109
make all; \
1110
cd ..; \
@@ -17,4 +16,3 @@ clean:
1716
make clean; \
1817
cd ..; \
1918
done
20-
rm -f $(ODIR)/*.o

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Kernighan and Ritchie The C Programming Language Code Examples
7171
* 4-08 - singlegetch.c: Implements getch() and ungetch() that support just a single char buffer
7272
* 4-09 - ungetseof.c: Implements getch(), ungetch(), and ungets() in a maner supporting EOF
7373
* 4-10 - rpncalc4-10/*.c: RPN Calculator (rpncalc5) based upon whole line handling
74-
* 4-11 - rpncalc4-11/*.c: RPN calculator (rpncacl6) that uses 'static' to negate needing ungetch()
74+
* 4-11 - rpncalc4-11/*.c: RPN calculator (rpncalc6) that uses 'static' to negate needing ungetch()
7575
* 4-12 - itoarecursive.c: A recursive implementation of itoa()
7676
* 4-13 - reverserecursive.c: A recursive implementation of reverse()
7777
* 4-14 - swapmacro.c: A version of reverse() that uses a macro for swaping two values

src/chapter05/strnfunc/Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ CFLAGS = -std=c99 -Wall -g -I.
66
LINT = splint
77
LINTFLAGS = -compdef +quiet +skip-sys-headers +charintliteral +charint -shadow -preproc
88
DEPS =
9-
_OBJ = strncat.o
10-
_BIN = strncat
9+
_OBJ = strncat.o strncopy.o strncmp.o
10+
_BIN = strncat strncopy strncmp
1111

1212
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
1313

@@ -32,6 +32,14 @@ strncat: $(ODIR)/strncat.o
3232
$(CC) $(CFLAGS) $^ -o $@
3333
cp -v $@ $(BIN)/$@
3434

35+
strncopy: $(ODIR)/strncopy.o
36+
$(CC) $(CFLAGS) $^ -o $@
37+
cp -v $@ $(BIN)/$@
38+
39+
strncmp: $(ODIR)/strncmp.o
40+
$(CC) $(CFLAGS) $^ -o $@
41+
cp -v $@ $(BIN)/$@
42+
3543
$(ODIR)/%.o: %.c $(DEPS)
3644
mkdir -pv $(ODIR)
3745
$(CC) $(CFLAGS) -c -o $@ $<

src/chapter05/strnfunc/strncmp.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* strncmp.c
3+
*/
4+
5+
#include <stdbool.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
#define MAXSTR 1000
10+
11+
/*
12+
* strncmp(): Compares the fist 'count' characters of 'source' string to 'dest' string.
13+
*
14+
* NOTE: A terminating '\0' *is not* appended!
15+
*
16+
* NOTE: It is assumed that 'leftstring' is big enough to hold the combined text
17+
*/
18+
19+
static int strncmp(char *s1, char *s2, size_t count) {
20+
for ( ; --count > 0 && *s1 == *s2; s1++, s2++)
21+
if (*s1 == '\0')
22+
return 0;
23+
return *s1 - *s2;
24+
}
25+
26+
27+
int main(void) {
28+
29+
char s1[MAXSTR] = "aaa";
30+
char s2[MAXSTR] = "aba";
31+
char s3[MAXSTR] = "abb";
32+
char s4[MAXSTR] = "aca";
33+
34+
printf("Comparing the first 2 characters of \"%s\" to \"%s\" results in: %d\n", s1, s2, strncmp(s1, s2, 2));
35+
printf("Comparing the first 2 characters of \"%s\" to \"%s\" results in: %d\n", s3, s2, strncmp(s3, s2, 2));
36+
printf("Comparing the first 2 characters of \"%s\" to \"%s\" results in: %d\n", s4, s2, strncmp(s4, s2, 2));
37+
38+
exit(EXIT_SUCCESS);
39+
}

src/chapter05/strnfunc/strncopy.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* strncopy.c
3+
*/
4+
5+
#include <stdbool.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
#define MAXSTR 1000
10+
11+
/*
12+
* strncopy(): Copies at most 'count' characters of 'source' string to 'dest' string.
13+
*
14+
* NOTE: A terminating '\0' *is not* appended!
15+
*
16+
* NOTE: It is assumed that 'leftstring' is big enough to hold the combined text
17+
*/
18+
19+
static void strncopy(char *dest, char *source, size_t count) {
20+
while (count-- > 0 && (*dest++ = *source++) != '\0')
21+
; // Empty
22+
}
23+
24+
25+
int main(void) {
26+
27+
char string1[MAXSTR] = "Overwrite me!!";
28+
char string2[MAXSTR] = "I am the destoryer!";
29+
30+
printf("Copies the first 4 characters of \"%s\" to the string currently containing \"%s\" results in: ", string2, string1);
31+
32+
strncopy(string1, string2, 4);
33+
34+
printf("\"%s\"\n", string1);
35+
36+
exit(EXIT_SUCCESS);
37+
}

0 commit comments

Comments
 (0)