lab06


 * CS 215 Spring 2012** **Lab 06** **"Strings and C-Strings"**

Due on Lab Day ( Tuesday 06/26 ) submitted before midnight Electronic submission to the CS portal: http://www.cs.uky.edu/csportal


 * Goals and Objectives **

In class you have been learning a bit about how C++ “objects” can be used to simplify operations. Clearly the **string** object you have been using up until now is in fact a perfect example of this: when you write “**string myString**;” in a program, you are actually declaring a variable that is an object, implemented for you, that can store individual characters. How is this object implemented? What structure is used within this object in order to provide the functionality of the “string” object? Arrays of **chars**, of course. An array of **char** is an array defined so that each slot holds a **char**. For example, this statement defines such an array: **char A[10];** When this array is “terminated”, which means that one of the slots of the array holds the special “null byte” character (**'\0'**), it becomes a special array of characters called a “C-String”. The array then represents a sequence of characters, from position zero in the array up to the place where the null byte is stored. C-Strings are often still used to represent a string. In fact the string object you are used to actually has a member function **c_string** that will return a c-string representation to help you use functions that require c-strings as parameters. As mentioned above, a C-String is an array of characters that ends with a null byte. Assume we have the following variables: char a = '0'; char b = '\0'; [that’s a zero in single quotes, not the letter “oh”] What is the difference between the null byte (b) and the character '0' (a)? (Hint: Use what you learned in the last lab to print the integer representation of both characters, and then use that information to answer this question).
 * Tasks **
 * Exercise 1:**

A C-String can be declared like this: char myString[5]; myString[0] = 't'; myString[1] = 'e'; myString[2] = 's'; myString[3] = 't'; myString[4] = '\0'; Or with the shorthand: char myString2[] = “test”; Note that in the second code sample, the compiler automatically appends a null byte to the end of the initialized c-string. So this means that the two c-strings created above are identical. Declare and initialize myString and myString2 as shown above. Now print out the comparison of the two c-strings (these arrays), but use the same approach/syntax that you would use on string objects, i.e., the **==** operator (myString == myString2). What happens? Why? You will discover that the **==** operator you have been using to compare two string objects until now has a special definition when used with two operands that are members of the **string** class. Because we are no longer using the string class, this operator does not do what you think it might. Try to explain the difference between what you think this operator does when comparing c-strings as opposed to string objects. Hint: think about arrays and pointers.
 * Exercise 2:**

Even though there are no implicit operator functions for manipulating c-strings like there are strings, there are functions that we can use. For instance the **strcmp** function can be used to compare two c-strings. Now type #include at the top of your program and print the following statement: cout << strcmp(myString, myString2) << endl; What is the result? Why? (Look up the definition of **strcmp** and describe its return value). As you may have guessed we can no longer use the assignment operator ( **=** ) to copy the value of one c-string to another; it also has a special definition relative to the string class. However, once again there is another useful function to help us: strcpy.
 * Exercise 3:**

Declare the following variables: char myString[10]; char newString[6];
 * Exercise 4:**

myString[0] = 'H'; myString[1] = 'e'; myString[2] = 'l'; myString[3] = 'l'; myString[4] = 'o'; myString[5] = '\0'; Is there anything wrong with calling the function strcpy(newString, myString);? Why or why not?

What if we changed myString[10] to myString[256]? In other words, make the "source string" a lot bigger than the "destination string" (the string into which we will copy the source). Does the compiler let us compile this code with **strcpy** copying into newString? Does the code run? Using what you know about the layout of arrays, what is happening here?
 * Exercise 5:**

Please turn in your answers electronically to each of the exercises using the CS215 webpage.