The goal of this lab is to practice working with pointers, dynamic allocation, and strings in C.
You will write some functions for a "homemade string" library.
Copy the given code for this lab:
mkdir lab7 cd lab7 cp ~tvandrun/Public/cs245/lab7/* .
Consider the two functions that are written for you.
hmstrcpy()
, "homemade string copy."
The standard strcpy()
, which we're imitating here,
takes two pointers, which it interprets as the starting addresses of
to areas of memory and copies the contents of the second area of
memory into the the first area.
Note how we know when we're done---we reach the end-of-string
marker.
We do not know when we reach the end of the array---the
array holding the string may be longer than the string it is holding.
(Or it can be shorter.... but that's bad.)
Notice that we do copy over the end-of-string marker itself.
hmstrcmp()
, "homemade string compare."
The standard strcmp()
, which, again, we're imitating,
takes two pointers to the beginning of strings and compares the contents
of the two strings, returning a negative number if the first comes first,
a positive number if the second comes first, and 0 if they are equivalent
(just like String.compareTo()
in Java).
Notice the handling of the end-of-string character.
hmstrlen()
.strlen()
that takes a pointer to the beginning of the string and computes its
length---the number of characters before the end-of-string marker.
hmstrcat()
.strlen()
does not make a new string.
Notice the return type here is void.
Instead it modifies the area of memory pointed to by
the first parameter.
Specifically, it finds the end of the string that starts at that
first address and then copies the contents of the string pointed to
by the second paremeter at the end of the first string.
Write an equivalent implementation for hmstrcat()
hmstrcat2()
.I have provided a makefile to compile your code and two driver programs.
Running make
will compile hmstring.c
and will also produce the excutable for hmstrexample
,
which runs various tests on your functions.