Activity 11-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:

// P11_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
             << 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.

The standard string class has several functions that make things very easy.  A list of these functions appear in the Display 11.7.  The following program illustrates a few examples.

// P11_2a.cpp - This program demonstrate 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
             << 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 11.2
I typed the following 2 lines late last night and made several mistakes.  As you will immediately notice, I have written G++ for g++ and Gcc for gcc.  I want you to help me fix these errors by writing a C++ program to replace the incorrect statements with the correct ones.  For now, assume that we will type these two lines from the keyboard.  So you will read the two sentences from the keyboard, and will display the corrected sentences at the end.

Use the above program and functions given in Display 11.7 to write a program that make the necessary corrections when these two sentences are type at the keyboard.  I hope to have your program handy so the next time I make such a mistake, I can easily fix it.

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.