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.
For example, if the parm contains
A | 5 | b | c | F | \0 | ? | ? |
where ? represents an unknown character, then lengthstring should return 5.
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 | ? |
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 | ? | ? | ? | ? |
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 ==>
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