Object Oriented Programming in C++ - Lab 1

 

A string is a sequence of characters. A string variable can hold data such as a name or an address. In C++ there are two ways to define strings. One is to use a class, which we will study later in the semester. The other way is to use C-style strings. A C-style string is an array of characters which is terminated by a special end-of-string value. This value is called the null byte, and can be written using the escape sequence '\0' or using the symbolic constant NULL. Any sequence of characters enclosed in double quotes is a string constant, and is stored by the compiler as an array terminated by a null byte.

The cin object has methods for reading into C-style strings. The extraction operator (>>) will read up to the next whitespace character (blank, tab, or newline). For example, suppose we have the input this is a string, and the following code:

   char message[50];
   cin >> message;

This will result in this being stored in message. To read in all characters on a given line (or up until a specified terminating character), use the cin.getline() method. This method is described on pages 634-636 of the Bronson book.

The cin.get() method can be used to input one character at a time into a string. See pages 641-643 in Bronson.

The cout object will output a C-style string. All characters before the null byte will be output.

In this lab you will write several functions to manipulate C-style strings. The main function will contain a loop which will allow the user to choose which of these functions to execute.

void readstring(char[])

  1. Read the string entered from the keyboard, one character at a time until the newline is entered. Return the string to the caller through the parm.
  2. Do not put the newline character into the string.
  3. Be sure to add the null byte ('\0') after the last character read into the array. (See page 641 in Bronson.)

int lengthstring(char[])

  1. The parameter is a C-style string. The function will count the number of characters in the string (up to, but not including, the null byte).

For example, if the parm contains

 A   5   b   c   F   \0   ?   ? 

where ? represents an unknown character, then lengthstring should return 5.

void copystring(char[], char[])

  1. Copy the contents of the second parm into the first parm.
  2. You may assume that the first array is large enough to hold the string in the second array.

For example, if first parm contains

 A   5   b   c   F   \0   ?   ? 

and the second parm contains

 1   2   4   8   16   32   \0   ? 

where ? represents an unknown character, then after execution of copystring, the first parm should contain

 1   2   4   8   16   32   \0   ? 

int concatstring(char[],char[])

  1. Concatenate the two parameters. Concatenate means to add one string onto the end of another string. In this function you will append the second string to the end of the first string.
  2. Make sure that the second string starts after the last character of the first string. There should be no null byte until the end of the second string.
  3. Make sure there is enough room in the first string to hold the concatenation. You can assume that the first string can hold up to 80 characters, so if the lengths of the two strings add up to 80 or less, there is enough room. Use the lengthstring function to get the lengths of the two strings.
  4. If there is not enough room for the concatenation, do not change the first string, and return false. If there is enough room, do the concatenation and return true.
  5. Be sure there is a null byte at the end of the concatenated string.

For example, if first parm contains

 A   5   b   c   F   \0   ?   ?   ?   ?   ?   ?   ?   ?   ?   ? 

and the second parm contains

 1   2   4   8   16   32   \0   ? 

where ? represents an unknown character, then after execution of copystring, the first parm should contain

 A   5   b   c   F   1   2   4   8   16   32   \0   ?   ?   ?   ? 

int main()

  1. Create three C-style strings called one, two, and three, which is each large enough to hold a string of length 80.
  2. Call the readstring function for strings one and two.
  3. Present the following menu repeatedly, until the user chooses Quit. (This is a perfect place to use do..while.)
              Please select one of the following:
                1: Compute the length of the first string
                2: Compute the length of the second string
                3: Copy the second string to the first string
                4: Concatenate the second string to the first string
                5: Quit
              Enter a number between 1 and 5 ==>
            
  4. If the user enters 1, call lengthstring with string one as the parm.
  5. If the user enters 2, call lengthstring with string two as the parm.
  6. If the user enters 3, call copystring with string three as the first parm, and string one as the second parm. After calling copystring, print string three.
  7. If the user enters 4, call concatstring with string one as the first parm and string two as the second parm. If concatstring returns false, print an error message. Otherwise, print string two.
  8. If the user enters 5, end the program.
  9. If the user enters a number less than 1 or greater than 5, print an error message and display the menu again.

Submission

Hand in a copy of your program and use the submit134 program to submit your code for testing and grading.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2005