Last updated: Thursday 6th July 2006, 14:01 PT, AHD

 

CSCI101
An Introduction to Programming using C++

 

Strings

 

             

1. String Basics

 

2. The C++ Standard string Class

             

 

1. String Basics

 

The C++ compiler provides two types of strings:

 

 

The first type is the type inherited from the C language, and variables of this type are referred to as cstrings.

 

The second type of string is generated from the string class and is discussed later in this presentation.

 

A familiar cstring example:

 

 

cout << "Hello World!";

 

cstring variables

 

In C++ strings can be stored in variables, and can be manipulated using string functions.

 

 

cstring variables

A cstring variable is exactly the same thing as an array of characters.

 

char a_string[]= "Anne";

 

 

character variables

Character variables (and predefined character functions) were covered in the chapter on I/O streams (section 3).

 

 

char a_string[]= "ANNE";

 

 

 

char b_string[5];

b_string[0] = '1';
b_string[1] = '2';
b_string[2] = '3';
b_string[3] = '4';
b_string[4] = '\0';


 

The '\0' character

'\0' is a single character (enclosed in single quote marks) which is a sentinel value (a flag) which marks the end of a cstring.

 

'\0' is called the null character

 

char a_string[]= "ANNE";

char b_string [5];
b_string[0] = '1';
b_string[1] = '2';
b_string[2] = '3';
b_string[3] = '4';
b_string[4] = '\0';


 

Example Program
declaring C string (cstring) variables

strings_1.cpp

 

legal C string (cstring) operations

strings_2.cpp

 

predefined C string (cstring) functions

strings_3.cpp

 

cstring functions strlen() and strcat()

strings_4.cpp

 

Predefined cstring functions

     strcpy(target_string, source_string)

  strcat(target_string, source_string)

  strlen(source_string)

  strcmp(string1,sting2)

 

                            

cstring arguments and parameters

A cstring variable is an array, so a cstring parameter to a function is simply an array parameter.

 

cstring arguments and parameters

As with an array parameter, whenever a function changes the value of a cstring parameter, it is safest to include an additional int parameter giving the declared size of the cstring variable.

 

cstring Input and Output

cstrings can be output to the screen using the insertion operator <<

 

cout << a_string;

 

cstring Input and Output

 

It is also possible to fill a cstring variable by using the extraction operator >>

 

BUT . . .

 

cstring Input and Output

When a cstring is read by using the extraction operator (>>), reading of the string stops at the first whitespace character.

A whitespace character is a space character, a tab or a new line.

 

Example Program
C string (cstring) input and output

strings_5.cpp

 

 

Example Program
C string (cstring) getline()

strings_6.cpp

 

 

End of String Basics

 

Strings

             

2. The C++ Standard string Class

 

Introduction

The cstring predefined string functions introduced earlier have some problems. . .

 

For instance, using:

strcpy(d_string,s_string)

 

if d_string is not big enough to hold s_string, this would usually result in a random area of memory being overwritten, and d_string is no longer a string as its last character is not '\0'. Unpredictable problems will occur.

 

The string class was designed to avoid the kinds of problems which may occur with cstrings.

 

The string class

The class string allows you to perform the same operations you can with cstrings, but a lot more besides.

 

There are well over 100 member functions associated with the string class.

 

With the cstring strings we used predefined functions to perform string copying and string concatenation (joining strings).

 

The standard string class uses the overloaded operator = to copy one string to another, eg:

 

d_string = s_string;

// copies s_string into

// d_string and ensures

// that d_string is big

// enough

 

 

Overloaded Operators

An overloaded operator is an operator which has been redefined so that it can be used in a different way to its original use, as well as retaining its original use.

 

Operators in C++

Operators in C++ (e.g. + - / % etc) are implemented very much like functions in C++.

 

And just like functions, operators can be overloaded.

 

The version of the operator which is actually used depends on the arguments given to it (just like overloaded functions).

 

 

The = (assignment) operator

The = operator is the assignment operator. Its normal use is to assign a value to a variable:

 

     age = 21;

 

 

The = operator

The = operator is not normally used with strings to assign a string value. For example, with cstrings you cannot do this:

 

name = "Jason";

 

The string class redefines (overloads) the assignment symbol (=) so that it has another meaning.

 

 

So if you declare a variable name to be of the string class, you CAN do this:

 

name = "Jason";

 

 

With the cstring strings we used predefined functions to perform string copying and string concatenation (joining strings).

 

The standard string class uses the overloaded operator + to add one string to another (concatenate), eg:

 

d_string = s_string + t_string;

// concatenates s_string

// and t_string into

// d_string and ensures

// that d_string is big

// enough

 

 

Overloaded Operators

An overloaded operator is an operator which has been redefined so that it can be used in a different way to its original use, as well as retaining its original use.

 

The + operator

The + operator is the addition operator. Its normal use is to add a value to a value or variable:

 

     total = price + tax ;

 

The + operator

The + operator is not normally used with strings to add string values. For example, with cstrings you cannot do this:

 

name = "Sam" + " Wong";

 

 

Using + with string objects

The string class redefines (overloads) the addition symbol (+) so that it has another meaning.

 

So if you declare a variable name to be of the string class, you CAN do this:

 

name = "Sam" + " Wong";

 

Constructors in the string class

The string class has a default constructor that initializes a string object to the empty string ("").

 

The string class has a second constructor which takes a single cstring argument and converts it to a string class object.

 

 

Using the string class

string phrase, word1("hot"), word2("dog");

phrase = word1 + " " + word2;

cout << phrase;

//

// outputs: hot dog

//

 

phrase = word1 + " " + word2;

The C++ compiler is doing a lot of work in the background when executing a line of the type shown above.

 

phrase,word1 and word2 are objects of type string.

 

The single space string " " is of type cstring.

 

 

String literals are cstrings

Any string constant

(a.k.a. literal),

e.g. "cat" and "dog"

is of type cstring.

 

 

phrase = word1 + " " + word2;

The overloaded + operator is a binary operator (i.e. it has two arguments, one at each side of the + sign).

 

 

phrase = word1 + " " + word2;

In the line shown above, the + is used twice. Once with a string on the left side and a cstring on the right, and again with a cstring on the left side and a string on the right.

 

 

phrase = word1 + " " + word2;

To cope with variations of use of the + operator for concatenating objects of type string, the + operator has three overloadings:

 

 

One overloading for a string on the left and a cstring on the right

one for the reverse situation

and one for a string on each side.

 

 

phrase = word1 + " " + word2;

Note that just as with the use of the + operator with additions, when used with strings, the operations are performed left to right. Parentheses are not required in order to force this order of execution.

 

The use of >> and << with objects of type string

 

Note that when you use << or >> with a string object, you are actually using an overloaded version of that operator. They work very much like they do with cstrings.

 

The overloaded >>

The overloaded extraction operator skips over whitespace. It reads to the next whitespace and discards it. So >> reads words with when used with objects of type string.

 

To read a line into a cstring variable:

To read a line into a cstring variable we had to use a member function of the istream class called getline( ).

 

To read a line into a string object:

For string objects you also use a function called getline(), but it is not a member function of the string class or any other class; - it is a standalone, predefined function. . .

 

The getline() for use with string objects

              getline(cin, word, 'X')

 

Example Program using the class string

 

 

strings_7.cpp

Member functions of the string class

See Appendix 4 of the textbook (p983, 6th Ed)

 

for examples of typical calls to

member functions of the class

string.

 

A selection of these are shown on

the next pages....

 

Calls to members of string class

 

Constructors:

 

string str; // default constructor

// creates empty string object

string str("string");

string str(aString);// aString is of type

// string

 

Calls to members of string class

 

Element access:

 

str[i]; // read-write access to character at

// position i

 

str.substr(position,length);

// returns a substring of str

 

Calls to members of string class

 

Assignment/modifiers:

 

str1 = str2; // assignment

str1 + str2; // concatenation

s.empty(); // returns true if s is empty

 

Calls to members of string class

 

Comparisons:

str1 == str2; str1 != str2;

str1 < str2; str1 > str2;

str1 <= str2; str1 >= str2;

 

str.find(str1)

//returns index of the first occurrence of str1 in str

 

 

 

This presentation uses the following files:

 

http://www.annedawson.com/strings_1.cpp

http://www.annedawson.com/strings_2.cpp

http://www.annedawson.com/strings_3.cpp

http://www.annedawson.com/strings_4.cpp

http://www.annedawson.com/strings_5.cpp

http://www.annedawson.com/strings_6.cpp

http://www.annedawson.com/strings_7.cpp