This laboratory examines details of string storage, and the operations of string library functions within the C programming language.
To get started, download the starter code for this lab: strings.tar.gz and run the following terminal commands:
$ cd csc161/labs
$ tar xvzf ~/Downloads/strings.tar.gz
$ cd strings
The .tar.gz file is a compressed archive that contains multiple files, a bit like the .zip files you may have used on other computers. The files in a .tar.gz archive will generally be contained within a directory, which in this case was named strings.
Before we get into complex examples, we’ll start by reading and predicting the behavior of code that uses pointers. Open and review string-intro.c.
Driver: Student closer to the whiteboard
first[3] = second[3] = third[3] = 0;
This line inserts a null character at index 3 for each of the three strings.
fifth[3] = 0;
fifth to include the const modifier:
const char * fifth = "Hello";
Consider the following string declarations.
char *baboon;
char *chimpanzee = "animal";
char dolphin[];
char emu[] = "animal";
char fox[4] = "animal";
char giraffe[8] = "animal";
char elephant[10];
elephant = "animal";
For the following exercises, make predictions. If you’re stuck or unsure about your predictions run the code to check your ideas.
Driver: Student closer to the whiteboard
sizeof operator tells you how much memory the compiler has
allocated to something.strlen function tells you the length of a string, which is the
number of characters before you hit the null character ('\0').Consider the following lines of code
char computerscience[16] = "isawesome";
char isawesome[16] = "computerscience";
printf("strlen (computerscience): %lu\n", strlen (computerscience) );
printf("strlen (isawesome): %lu\n", strlen (isawesome) );
printf("computerscience: %s\n", computerscience );
printf("isawesome: %s\n", isawesome );
Driver: Student farther from the whiteboard
What do you predict will be the output of the program above?
Start a new program for several experiments with strings. Copy the declarations and code above into a main procedure, making sure that you include the library string.h. Verify your previous prediction by compiling and running your program. Briefly explain why these results are printed.
char computerscience[16];
instead of:
char computerscience[16] = "isawesome";
computerscience[16] initialization and then add this
line of code to the end:
printf("Concatenate the strings: %s\n", strcat (isawesome, computerscience));
isawesome to 32?strcat() do? Summarize
conceptually what happens in the array and where the null
character(s) is/are?Strings can be passed to a function in a similar way as arrays.
Driver: Student farther from the whiteboard
/**
* Revers a string
*
* \param str[] The string to reverse
*/
void
string_reverse (char str[]);
It should reverse the order of the characters in str (except the
null character). Note that it will not return a new string, but it
will modify the given string.
Note: The function strlen is “expensive” because it must
traverse the entire string searching for the null terminator. Thus,
your solution should call it at most once.
The string.h library provides a set of useful functions, among them:
strlen(s) returns the length of sstrcmp(s,t) returns 0 if two strings are the same, a value smaller
than 1 if s<t , and a value greater than 1 if t<s.strcpy(s,t) copies the string t to s.Driver: Student closer to the whiteboard
The terminal command man provides helpful information about the
standard library C functions.
man string. You will see list of functions available in the string library. Remember that to quit the man pages, you can simply type q.man strcmp. What are the two
different ways that you can compare two strings?strncmp.man strcat. What does strcat do? Using what you have learned about how
strings are stored, and their null characters, explain how
strcat works.