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 that works like the standard C library's functions for manipulating arrays of characters as strings.
Clone the repository to get the code for this lab:
hg clone /cslab/class/csci245/lab7
Now you can cd
into the lab7
directory.
What you have are the header file and implementation for our "homemade string" library. 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
two 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 character '\0'
that
is 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 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.
You can compile the example program with make
. That
will compile the example program in hmstrexample.c
, which
you can run with ./hmstrexample
. The command
./strexample
will run an equivalent program that uses the
standard C library functions. Those two programs should produce the
same output--once you complete this lab.
hmstrlen()
"Homemade string length."
Write an imitation of the standard string function 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()
"Homemade string concatenation."
Unlike the string concatenation operation in Java,
C's 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 parameter at the end of the first string.
Write an equivalent implementation for hmstrcat()
It is probably a good idea to draw a picture here. How big does the first array need to be if this is not going to go out of bounds?
hmstrcat2()
Now, just for fun, write a second version of "homemade string concatenation"
that works more like string concatenation in Java.
Do not modify either of the strings given, but instead, allocate
a new dynamic charater array, big enough to handle
all the characters in both strings total, and then
copy all the characters from the old strings to the new.
Finally, return the pointer to this newly allocated character array.
Draw a picture to make sure you understand this. Be careful to allocate exactly enough characters (not too many, not too few) to hold the new string.
I have provided a makefile to compile your code and two driver programs.
Running make
will compile hmstring.c
and will also produce the executable for hmstrexample
,
which runs various tests on your functions. As supplied, this program
and strexample
should produce the same output once your
functions are complete.
Feel free to add other tests/examples to
hmstrexample.c
.
hmstring.c
. Also be sure that you have all
of your work committed to Mercurial. Then do
make handinto get it handed in. Check for a message indicating success, and make sure that the revision submitted is your latest code.