The goal of this lab is to learn the basics about strings and structs in C.
You are writing software for a Twitter knock-off called Chatter. In the Chatter system, users send messages at most 140 characters long. Additionally, each message has the user id of the user who sent the message (at most 16 characters long), a time stamp indicating the time the message was sent (a long int indicating the number of milliseconds since midnight, Jan 1, 1970), and up to 5 hash codes indicating categories or topics the message falls under (each hash code is at most 10 characters long).
You have been provided some code for producing "random" text components and "random" user names. Your tasks will include designing and implementing a struct to represent messages, writing functions for displaying and sorting messages, and completing drivers to test these messages.
Set up a new directory for this lab and move into it.
mkdir lab3 cd lab3
Copy starter code from the course directory.
cp /cslab/class/csci245/lab3/* .
In order to do this lab, you need to know a little more about how
strings work in C. Recall that there is not really a string type (in
the style of Java class String
); all that we have in C
are:
char
as
a string.
To use an array of characters as a string, it has to contain a special
character to mark its end; that character has the integer value
0
, which as a char
would be written
'\0'
. The historic name for that character is
NUL, though it is more commonly spelled null, and
so these strings are usually described as null-terminated.
When you write a string literal the
array value created for it includes the terminating zero; so, for
example, the literal "foo"
specifies an array of
four characters: 'f'
, 'o'
,
'o'
, '\0'
. So when we talk about a string
of length 7 (or containing 7 characters), that requires a
char
array of size 8.
There are library routines that let you do useful things with
strings; they are declared in the include file string.h
.
The ones you might find useful in this lab are
int strcmp(char a[], char b[])
a
is less than, equal to, or
greater than b
.
strcpy(char dest[], char src[])
src
to
dest
. (For the time being, you can ignore the fact
that this function does return a value.)
int strlen(char s[])
s
, not
including the terminating 0.
There are a few things that require you to be very careful:
strcpy()
does not make any space for the
destination. You must have already declared an array that is large
enough to hold the one being copied--including the terminating 0.
If you try to copy a string to an array that is too small, you will
make a mess of your program.
Note that because strings are character arrays, you can manipulate
the individual characters. That means that you can make the string in
a character into a zero-length string (an empty string) by assigning a
'\0'
to element 0.
Keep in mind that you cannot use the comparison operators
(such as >
or ==
) on string values.
Also, remember that strcmp()
is not a comparison for
equality; if you want to test whether two strings are equal, you have
to something like
strcmp(a, b) == 0
You will find more information about strings are in chapter 4 of On to C
.
The chatterUtil
library provides the following
functions:
randomInt()
: produce a random int
between 0 (inclusive) and a given int (exclusive).
So randomInt(4)
will return 0, 1, 2, or 3,
with equal probability.
getTimeMillis()
: return the current time
fillRandomText()
: fill the given char
array with a string containing a randomly chosen text
for a chatter message
fillRandomUserName()
: fill the given
char array with a string containing a randomly chosen
text for a user name
fillRandomHashCode()
fill the given
char array with a string containing a randomly chosen
hash code
You need to complete the following tasks:
message.h
). I have already set up a dummy
struct for a struct message
type.
You need to complete that struct according to
the description of a message given above.
Since the struct type needs to be seen by
the library and the drivers, it is defined in
message.h
message.c
).
The header file message.h
gives the prototype for the function newRandomMessage()
which is used by the two driver programs.
Write a definition for this function in an
implementation file for the message library.
Use the functions from chatterUtil
to
produce the text, user name, and hash codes.
Use randomInt()
to determine the number of
hash codes.
message.c
).
The headerfile message.h
gives the prototype for the function display()
which displays a message to the screen, nicely formatted,
showing all the information stored about that
message.
The driver displayDriver
tests this
(as well as your newRandomMessage
).
The display of a message should look something like this:
----------------------- Sent by Verna at 1315499047608 Q: How can you tell if an elephant is in the refrigerator? A: The door won't shut. #mischief #concert #admiration #concertIf you want the date to appear as something nice looking instead of a number of milliseconds, see below.
message.c
).
The headerfile message.h
gives the prototype for the function sortByUserName
,
and the driver sortDriver.c
uses it.
This function must be completed so that it
sorts an array of messages by user name, alphabetically.
You may use any sorting algorithm for this.
What will be different is that you are sorting strings,
not numbers.
To do this alphabetically, first look at the first character of each
string---remember that characters are represented by numbers,
and the numeric code for characters is consistent with
their alphabetical ordering.
If the first character is a tie, then look to the second
character, etc.
Note that sortByUserName()
also needs
the size of the array as a parameter.
sortDriver
.
The sortDriver
program
is intended to display all the usernames in the
array of messages after sorting,
to search the array for a message containing
a given hashcode, and then display those messages.
You may choose the hashcode to search for.
Finish the driver so that it exercises these
things.
(You may add appropriate functions to the message
library if you think it will make the task easier.)
Edit your makefile to handle sortDriver.o
and sortDriver
.
If you want to make a readable formatting of the time stamp, do the following things to your code:
sys/time.h
in message.h
time.h
in message.c
time_t
instead of long int
newRandomMessage()
,
don't use the getTimeMillis()
function that I provided but instead
use the function time()
.
Pass it the timestamp portion of the struct preceded by an ampersand.
For example, time(& (toReturn.timestamp));
display()
, use the function ctime()
to convert time to a string.
For example,
printf("Sent by %s at %ld\n", msg.uid, ctime(&(msg.timestamp)));
Then the display of a message would look something like
----------------------- Sent by Willis at Thu Sep 8 12:32:37 2011 Q: Why do elephants stomp on people? A: They like the squishy feeling between their toes. #surplus #moisture
Be sure that your names are in all of the files you modified. Turn in your files with
/cslab/class/csci245/bin/handin lab3 makefile message.h message.c sortDriver.c