Computer Science 101Seventh Lab Assignment |
Week of November 4th |
As with all labs, the expectation is that you will complete this assignment during this 2-hour lab period. When you have finished, you will need to demonstrate that fact to either Bart or Dianne. Should you not finish within the 2-hour block, you are free to work on it over the course of the week and demonstrate it in next week's lab period.
There is a homework assignment which corresponds to this lab. It is due in two weeks, in lab. See the syllabus for late homework policies.
The goal of this lab assignment is for you to gain experience with character arrays or strings. They are not so different from regular arrays, as this assignment will illustrate, so your array skills will be honed as well.
This lab also introduces you to %s, which is analagous to
%d or %c but for strings.
The goal of this assignment is for you to write a program that asks a user for two words, then compares the words and reports whether they are the same or not. The outline of the program is:
The bulk of the work is the comparison of two strings. While you
could use strncmp to do this comparison, I want you to
write your own string comparison function. To test if two strings
are equal, you simply must compare the letters at each position.
The other tricky part of the assignment is how to read in a word from
the user. scanf is how we have read in numbers and
characters, and it is what we use to read words as well. But this
time we want to read strings, so we must use %s rather
than %d or %c.
In the past we have passed scanf the address of the
variable into which we would like the value placed. For example
When reading strings, we pass
int x;
scanf( "%d", &x );
scanf the address of the
array into which we would like the string of characters placed. Since
the name of an array is infact a pointer to the first element,
i.e. its value is the address of the first element, we do not need the
&. Therefore the following would work
char word[ 20 ];
scanf( "%s", word );
There is a problem with the above example. What happens when the user
types in a word with 22 characters? Try it - the program is likely to
crash. scanf, as written above, does not check to see
that the array it is writing values into is actually large enough to
store the values it is reading. In fact it cannot make that check
since it has no way of knowing the size of the array. There is,
however, a way to limit how many characters scanf reads.
That syntax can be found online or in the book (at the beginning of
the chapter on Strings).
A perhaps better solution is to not use scanf but rather
use fgets. If you finish this lab using
scanf and wish to explore fgets, you could
look at page 443 in the book or on the web for more information. Here
is a correct version of the above code, using fgets:
This code uses
char word[ 20 ];
fgets( word, sizeof( word ), stdin );
sizeof, a C operator which determines the
number of bytes allocated for a particular variable at runtime. It
works here because we statically allocated word.
sizeof will not work for the dynamically allocated
variables we will see later. Again, do not worry about this unless
you have finished your lab.