CMPT 130 Lab 5 – Introduction to Strings

 

In assignment 3 you will have to perform some basic manipulations of string objects. This lab introduces the string class and gives you some practice using it so that you have enough information about strings to complete assignment 3.

 

Labs are assessed so make sure that the TA has seen, and marked, your finished work before you leave the lab.

 

String Variables

A string is a sequence of characters. Single characters are enclosed in single quotes, like 'a'. Strings are enclosed in double quotes, like "Bob" (or "a"). A character variable (type char) can only ever store a single character. A string variable can store as few as zero characters (the empty string) and as many as the system dependent maximum string length. This is very large, usually in excess of four billion characters!

 

Strings are not C++ base types (like int, double, float, bool, char and others). A string variable is an example of a complex variable whose type is a class. We will discuss classes later in the course, but they are referred to as complex because they have more than one attribute, or value, associated with them. A string variable has an associated string (the sequence of characters) which is its primary values, it also contains an attribute to record its size. Note that variables whose type is a class are usually referred to as objects, but for the purposes of this introduction we will continue to refer to them as variables.

 

Since strings are not base types we need to include the string library to use them. The string library is part of the std namespace. Here is a short program that creates and prints a string variable

 

#include <iostream>

#include <string>

using namespace std;

 

int main()

{

     string name = "bob";

     cout << name << endl;

     name = "kate";

     cout << name << endl;

 

     cout << endl << endl;

     return 0;

}

 

Copy and paste the above program into a .cpp file and compile and run it.

 

There isn't much going on in this little program. However, you can see that string variables are declared just like any other variables and can be initialized, assigned new values and printed using cout. If you assign a string to a string variable it must be enclosed in ""s. If you want a string variable to be empty you can assign it the empty string:

 

name = "";

 

Concatenating and Returning Strings

Strings can be returned from functions just like any other variable type in which case the function's return type should be string. In this section you will write a function to return the user's name – that is, their first and last names separated by a space. The function prototype should look like this:

 

string getName();

 

In the body of the function you should have two sets of cout and cin statements that request the user to enter their first and last names and that input the names to string variables called first and last. You then need to return a single string that contains these two values separated by a space. You can do this by concatenating strings together. Concatenation is the process of adding one string to the end of another. In C++ the concatenation operator is +. Note that this is a re-use of the addition operator. This is not confusing for the compiler as the version of the operator that is used is dependent on the types of the operands.

 

You cannot just concatenate the first and the last names like this:

 

return first + last;

 

This will compile and run but will result in a name string with no space separating the two names (like JohnEdgar). Fortunately, you can concatenate double quoted strings (called string literals) in the same way as concatenating string variables. We want to return first name, a space and last name. So, the return statement of your function should be:

 

return first + " " + last;

 

Amend the program from the first section to add a call to your getName function and then print the result – like this:

 

name = getName();

cout << name << endl;

 

Here is a sample run of the modified program

 

bob

kate

Enter your first name: John

Enter your last name: Edgar

John Edgar

 

Incidentally, remember that the result of calling a function that returns a value is that the function call is replace by its return value. This means that you can concatenate the return value of a function that returns a string with another string. You could therefore do something like this:

 

string sentence = "My name is " + getName();

 

This isn't something I'm asking you to do in the lab but something like this may be useful in assignment 3 …

 

Concatenating Strings and Numbers

Some programming languages allow you to concatenate strings and numbers together:

 

string name = "Bob";

string bobAge = name + 23; // error

 

C++ does not allow mixed types in a concatenation operation like this and the lines shown above would result in a compilation error. This is not something you will need to do the assignment 3 so it will not be discussed further in this lab.

 

Another Concatenation Operator

This is all you need to know about strings to complete assignment 3. We will be discussing strings in greater detail later in the course.

 

Just like the shorthand for assignment and addition, +=, the same operator can be used for concatenation. This means we could have written the body of the getName function in a slightly different way.

 

string result, first, last;

// get first name and store in first

result += first;

// get last name and store in last

result += " " + last;

return result;

 

This isn't a better version than the one described in the previous section, it is just intended to demonstrate a slightly different solution to the same problem. Note that there is no need to initialize the result string. Unlike base type variables, class variables are initialized with a default value. For strings this default is the empty string.

 

Assessment

1 mark for completion of the lab.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)