Chapter 15 – Strings

15.1  String Variables

To store word and sentences, you can use the string class.  String is not a fundamental C++ variable type, but a class you can create objects from.  The data is actually an array of characters (char).  A string can store things such as "Hello" or "I am 21 today!".  Strings are enclosed in quotations (double quotes) – unlike char variables which are enclosed in apostrophy's (single quotes).

The program below shows how to declare a variable of type string, assign it a value (Hello World!), and print it out.

Program 15.1a – Declaring and Using a String

#include <iostream>
using namespace std;
int main()
{
   string A = "Hello World!";
   cout << A << endl;
   return 0;
}

Output
Hello World!

The next program shows how strings can be concatenated (joined together) using the plus sign.  Notice how a space between the two words was included by placing it in the double quotes " ".

Program 15.1b - Concatenating Strings

#include <iostream>
using namespace std;
int main()
{
   string A = "Hello";
   string B = "World";
   string C = A + " " + B + "!";
   cout << C << endl;
   return 0;
}

Output
Hello World!

Strings can be compared.  The next program compares "apple" to "apricot" to see which one is greater.  When comparing strings, the ASCII number for each letter is compared (refer to the ASCII chart).  Try running the same program with "Apricot" capitalized.

Program 15.1c - Comparing Strings

#include <iostream>
using namespace std;
int main()
{
   string A = "apple";
   string B = "apricot";
   if (A < B)
      cout << A << " is less than " << B << endl;
   else
      cout << A << " is greater than " << B << endl;
   return 0;
}

Output
apple is less than apricot

15.2  Cin Statement with Strings

The cin statement will truncate a string at the first space as shown in the program below.

Program 15.2a - String Truncation

#include <iostream>
using namespace std;

int main()
{
   string S;
   cout << "Enter a sentence: ";
   cin >> S;
   cout << "Your sentence: " << S << endl;
   return 0;
}

Output
Enter a sentence: Have a nice day!
Your sentence: Have


One way to fix this problem is to use the getline function as shown below.

Program 15.2b - No String Truncation

#include <iostream>
using namespace std;

int main()
{
   string S;
   cout << "Enter a sentence: ";
   getline(cin, S);
   cout << "Your sentence: " << S << endl;
   return 0;
}

Output
Enter a sentence: Have a nice day!
Your sentence: Have a nice day!

15.3  String Functions

When you declare a string, you are creating an object of the string class (instead of declaring a primitive variable).  Since string is a class, there are many functions you can use.

Assume:    string A = "have a nice day";

Function Returns Description
A.size() 15 Returns the number of characters in A (including spaces)
A.substr(7,4) "nice" At position 7, returns 4 characters
A.find("n",0) 7 Returns the location of the first "n" beginning at postion 0.  If it is not found, it returns -1.
A.find("a",7) 13 Returns the location of the first "a" beginning at postion 7.
A.insert(7,"very ") "have a very nice day" At position 7, insert "very ".  Important: The insert function changes the value of A.
A.replace(7,4,"swell") have a swell day At position 7, replace the 4 characters ("nice") with "swell".  Important: The replace function changes the value of A.

The programs below demonstrate the string functions.

Program 15.3a - Substring Function

#include <iostream>
using namespace std;
int main()
{
   string S1 = "0123456789";
   string S2 = "good morning";
   cout << "Today I am " << S1.substr(2,2) << " years old.\n";
   cout << "You are a " << S2.substr(0,4) << " neighbor.\n";
   return 0;
}

Output
Today I am 23 years old.
You are a good neighbor.

 

Program 15.3b - Insert Function

#include <iostream>
return 0;

int main()
{
   string X = "happy birthday";
   cout << X.insert(0,"have a ") << endl;
   string Y = "very ";
   X.insert(7,Y);
   cout << X << endl;
   return 0;
}

Output
have a happy birthday
have a very happy birthday

 

Program 15.3c - Replace Function

#include <iostream>
using namespace std;

main()
{
   string X = "It is time for class";
   X.replace(11, 3, "to go to");
   cout << X << endl;
   X.replace(3, 2, "will be");
   cout << X << endl;
   return 0;
}

Output
It is time to go to class
It will be time to go to class

 

Program 15.3d – Search and Replace

#include <iostream>
using namespace std;

int main()
{
   string A = "This is a search and replace example";
   int X = A.find(" ", 0);
   while (X < A.size())
   {
      A.replace(X,1,"-");
      X = A.find(" ", X+1);
   }
   cout << A << endl;
   return 0;
}

Output
This-is-a-search-and-replace-example


 

Exercises – Chapter 15

What will the following cout statements print?

#include <iostream.h>
#include <string>

int main()
{
  string A = "good morning";
  string B = "alphabet soup";

  cout << B[0] << B[1] << B[2];

  cout << A.substr(0,4) << B.substr(9,4);

  cout << A.substr(5,3) << B[6];

  A.replace(5,3,"eve");

  cout << A;

  cout << A.find("e",0);

  cout << B.substr(0,5);

  A.insert(0,"very ");

  cout << A;

  return 0;
}

 

 

 

 

5. (a) goo   (b) mor   (c) alp

6. (a) goodsoup   (b) good soup

7. (a) mornbe  (b) more  (c) mor

 

8. (a) good evening  (b) good eve

9. (a) e  (b) 5  (c) v  (d) 7

10. (a) alpha  (b) good  (c) bet

 

11. (a) very alpha
    (b) very good morning
    (c) very good evening