Activity 10-2 - The Standard String Class



In the previous activity, you learned to create C-strings.  The C-strings were simply arrays of characters terminated with the null character, '\0'.  To manipulate these strings you used some functions. In almost all cases, you as the programmer need to somehow keep track of number of elements that you have stored in a C-string. The standard string class is defined in the library <string> and the definitions are placed in the std namespace. The class string allows you to treat string values and string expressions very much like values of a simple type. For example, when s1, s2, and s3 are objects of type string, to concatenate string s2 at the end of string s1 and to store the resulting string into s3, we will use:

s3 = s1 + s2;

Also, to initialized a string s4, we no longer need strcpy function and we can use:

s4 = "Hello World";

The string class has a constructor that initializes the string to empty and one that initializes the string to a desired string. Following are the examples for these two:

string empty_string;
string something("hello");

The first one uses a constructor to create the empty_string as an empty string and the second one initializes the string to "hello".  The following two lines are equivalent:

string something("hello");
string something = "hello";

Here is an example in which a string class is used:

// P10_2.cpp - This program demonstrates the use of string classes

#include<iostream>
#include<string>
using namespace std;

int main( )
{
     string phrase;
     string adjective("fried"), noun("ants");
     string wish = "Bon appetite!";

     phrase = "I love " + adjective + " " + noun + "!";
     cout << phrase << endl;
     cout  << wish << endl;
     return 0;
}

As you may have noticed, in this program "+" is overloaded in the string class such that it now does the concatenation of strings (overloaded means that. the same symbol is used for more than one operation, for example the symbol '+' is used for the classic addition operation and in this case the same '+' symbol it is used for the concatenation of strings operation).

The standard string class has several functions that make things very easy. A list of these functions appears in the Display 8.7 of your book (or slide 87 of your PowerPoint notes).  The following program illustrates a few examples.

// P10_2a.cpp - This program demonstrates the use of functions associated
// with the string class

#include<iostream>
#include<string>
using namespace std;

int main( )
{
     string phrase,temp1, temp2;
     string adjective("fried"), noun("ants");
     string wish = "Bon appetite!";

     phrase = "I love " + adjective + " " + noun + "!";
     cout << phrase << endl
     cout << wish << endl;

      // copy phrase to newphrase
      string newphrase(phrase);

     // append wish to the end of newpharase
      newphrase += wish;

     // separates the first sentence
     temp1 = newphrase.substr(0, 18);
     cout << temp1 << endl;

     // separates the second sentence
     temp2 = newphrase.substr(18, 10);
     cout << temp2 << endl;

     return 0;
}

In the above program the statement:
     temp1 = newphrase.substr(0, 18); and
     temp2 = newphrase.substr(18, 10);

have something new in them.  The substr(position, length) function is a function that returns the substring of the calling object starting at position and having length characters.

Exercise 10.2
I typed 2 lines late last night and I made some mistakes.  These are the lines I typed:

The C and C++ compilers are integrated; G++ is a script to call Gcc with options to recognize C++.
Gcc processes input files through one or more stages.

As you probably have noticed, I have written G++ for g++ and Gcc for gcc. Write a C++ program that reads the two lines from the keyboard and displays the corrected lines on the monitor. Get inspiration from the above program and use the functions given in Display 8.7 to write a program that makes the necessary corrections. Call your program ex102.cpp.