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.
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.
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:
string one = "Harvey"; cout << one.size () << endl;
prints 6, the number of characters in Harvey.
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.
string one = "Harvey"; cout << one [2] << endl;
prints out r, the number 2 character in Harvey, with counting starting at 0.
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.
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.
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.
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;
}