String objects String objects

1  string data and char data

Char data is stuff like H, a, ?, a blank space, or a special character like '\ t' (which means tab), or '\ n' (which means new line).

C++ knows something is char data by single quotes: 'H', 'a', '?', '\ t', and '\ n'.

To store char data do this: char theChar = 'H';

String data is stuff like harvey or This is a string

C++ knows something is a string data by double quotes: "Harvey", and "This is a string"

To store string data do this: string theString = "Harvey"; But for this to work you must add #include <string.h> to your program.

Why is the #include needed for string? It turns out that a string is something beyond a char or int or double or float.

2  Objects and services

What is an object?

Think about your stereo. You tell it you want music (by pressing a couple of buttons) and it plays music for you. You press a couple more buttons and it stops. What happens inside after you press the buttons? You don't know. (And you don't want to know.) Your stereo is an unknown mechanism that offers services to you (music) when you tell it to. This is just what an object is.

An object is an unseen mechanism that offers a menu of services to you. You get the services by telling the object what you want. The services are really functions. For the computer to understand the functions it needs an #include line.

3  String objects and their services

For Visual C++ to understand the services offered by string objects, your program should start as follows:

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

The services offered are:

  1. The string object can tell us how large it is.

    			string one = "Harvey";
    			
    			cout << one.size () << endl;
    		

    prints 6, the number of characters in Harvey.

  2. The string object can allow us to stick together (concatenate) strings and chars together.

    			string one = "This is", two = "a string";
    			
    			cout << one + ' ' + two << endl;
    		

    prints out This is a string

    Note: cout << "This is" + ' ' + "a string" << endl; will not work because "This is" and ä string" are string data, not string objects.

  3. The string object can allow us to pick single characters out of the string.

    			string one = "Harvey";
    			
    			cout << one [2] << endl;
    		

    prints out r, the number 2 character in Harvey, with counting starting at 0.

  4. The string object allows us to pick out substrings of the original string.

    			string one = "This is a string";
    			
    			cout << one.substr (5,8) <<endl;
    		

    prints out 8 characters from the string starting with the character at position 5 (positions start with 0). So is a str is what gets printed.

  5. The string object allows us to find where a substring starts in the given string.

    			string one = "This is a string";
    			
    			cout << one.find ("is a", 0);
    		

    prints out the number 5, which is the position where is a begins in This is a string.

  6. The string object allows us to input a whole line at a time.

    			string one;
    			
    			getline (cin, one, '\n');
    		

    uses cin to take all characters up to the end of the line (indicated by the '\n'), and puts them into the string called one.

3.1  You do it

  1. Write a program to

    1. Store ``Play it again, Sam'' in a string.
    2. Print the character at the 5th position.
    3. Print the number of characters in the string.
    4. Store the position where ``Sam'' begins.
    5. Store the part of the string before ``Sam''.
    6. Have the user input a name and store it in a string.
    7. Store a new string that consists of the string you stored with the inputted name stuck on the end of it.
    8. Print out the new string.

4  A string example

The task: to take of the form Joe Bloggs and reform it to be Bloggs, Joe.

Here is the plan:

Get name input Pick off the first name Pick off the second name Assemble in second-comma-first order and print

Here is the program:

	// A program to rewrite firstName secondName in the form of 
	// secondName, firstName.
	
	#include <iostream>
	#include <string>
	
	using namespace std
	
	string GetName              ();
	string GetFirstName         (string);
	string GetSecondName        (string);
	string MakeSecondFirstOrder (string, string);
	
	int main ()
	    {
	    string name       = GetName ();
		
	    string firstName  = GetFirstName (name);
		
	    string secondName = GetSecondName (name);
		
	    string newName    = MakeSecondFirstOrder (firstName, secondName);
		
	    cout << "\n\n"
	         << newName
	         << endl;
		     
	    return 0;
	    }
		
	string GetName ()
	    {
	    string name;
	    
	    cout << "Enter name: ";
	    
	    getline (cin, name, '\n');
	    
	    return name;
	    }
	    
	string GetFirstName (string name)
	   {
	   //Find the location of the space between the names
	   
	   int spaceLocation = name.find (" ",0);        
	                                  
	   //Cut out the first name
	                   
	   string first = name.substr (0, spaceLocation);   
	                               
	   return first;
	   }
	   
	string GetSecondName (string name)
	   {
	   int spaceLocation = name.find (" ",0);   //Find the location of the
	                                            //  space.
	   
	   int lastLocation = name.size () - 1;     //Calculate position of the last
	                                            //  character in the full name
	                                            //  (counting starts at 0)
	                                                                           
	   //Cut out second name
	                                                                                         
	   string second = name.substr (spaceLocation + 1,             
	                                lastLocation - spaceLocation);   
	   
	   return second;
	   }

	string MakeSecondFirstOrder (string first, string second)
	   {
	   return second + ", " + first;
	   }

4.1  You do it

  1. Write a program to input a word from the user and print out the Pig Latin equivalent. For our purposes, one turns a word into Pig Latin by

    1. moving the first character to the end of the word.
    2. adding ``ay''.

  2. Write a program to input a two word phrase from the user and print out the phrase with both words turned into Pig Latin.


File translated from TEX by TTH, version 2.25.
On 7 Oct 2002, 18:57.