Last modified: May 28, 2014
One of the most commonly used data types is the character string.
In C++, string is NOT a built-in data type.
The string class supports the following member functions
non-default constructor which allows definition from a string literal or another “string” object
length function
append function to add to the end of a string
overload to the “+” operator to concatenate two strings
overload to the relational operators for string comparison
A variety of find… functions for searching through strings.
A substr function to extract portions of a string.
#include <string>
#include <iostream>
using namespace std;
int main()
{
string firstName, lastName, fullName;
string greeting("Hello ");
cout << "What is your name (first last separated by a space)? ";
cin >> firstName >> lastName;
greeting.append(lastName);
greeting.append(", " + firstName);
string banner(greeting.length() + 4,'$'); // construct string with bunch of '$'s
cout << banner << endl;
cout << "$ " << greeting + " $" << endl;
cout << banner << endl << endl;
if(firstName < lastName)
cout << "your first name is alphabetically before your last\n";
else
cout << "your first name is alphabetically after your last\n";
return 0;
}
Sample Execution
What is your name (first last separated by a space)? chris wild
$$$$$$$$$$$$$$$$$$$$$
$ Hello wild, chris $
$$$$$$$$$$$$$$$$$$$$$
your first name is alphabetically before your last
There are three approaches used for reading strings.
Whitespace characters are: blank, tab and new line.
Use the regular istream extraction operator (‘>>’) for this purpose.
Initial whitespace characters are ignored. (Try running the above program and adding extra spaces between your names.)
One problem with this approach is that you cannot read a string with blank characters in it.
The following program should produce the same output as the first program.
#include <string>
#include <iostream>
using namespace std;
int main()
{
string firstName, lastName, fullName;
string greeting("Hello ");
cout << "What is your name? ";
getline (cin, fullName);
if (fullName.size() > 0 && fullName[0] == ' ')
{ // Trim leading blanks from fullName
int charPosition = fullName.find_first_not_of (" ");
fullName = fullName.substr(charPosition);
}
// Split fullName into parts
int blankPosition = fullName.find(' ');
if (blankPosition != string::npos)
{
firstName = fullName.substr (0, blankPosition);
int charPosition = fullName.find_first_not_of (" ", blankPosition);
lastName = fullName.substr (charPosition);
}
else
lastName = fullName;
greeting.append(lastName);
greeting.append(", " + firstName);
string banner(greeting.length() + 4,'$'); // construct string with bunch of '$'s
cout << banner << endl;
cout << "$ " << greeting + " $" << endl;
cout << banner << endl << endl;
if(firstName < lastName)
cout << "your first name is alphabetically before your last\n";
else
cout << "your first name is alphabetically after your last\n";
return 0;
}
We use the function getline for this purpose.
getline will put all characters into the string including blanks and tabs (but not newline characters).
But, because we have acquired the entire name in a signle string value, we now need to use string functions to split the name into the desired pieces.
#include <string>
#include <iostream>
using namespace std;
int main()
{
string firstName, lastName, fullName;
string greeting("Hello ");
cout << "What is your name (first last separated by a space)? ";
getline (cin, firstName, ' ');
getline (cin, lastName);
cin >> firstName >> lastName;
greeting.append(lastName);
greeting.append(", " + firstName);
string banner(greeting.length() + 4,'$'); // construct string with bunch of '$'s
cout << banner << endl;
cout << "$ " << greeting + " $" << endl;
cout << banner << endl << endl;
if(firstName < lastName)
cout << "your first name is alphabetically before your last\n";
else
cout << "your first name is alphabetically after your last\n";
return 0;
}
This code is simpler, but also more “fragile” than the version before. If the person actually types blanks before their first name, we are in trouble. If the person types multiple blanks between the two parts of their name, we are likewise in trouble.
So which method of inputting strings is the best to use?
The answer depends on how much you know about your input. Use >> when you know you want to skip leading whitespace and that you won’t have whitespace inside the value you want to read.
Use getline when you want an entire line of data.
If you want a partial line of data that stops at something other than whitespace, use the 3-parameter form of getline.
Remember always that >> skips over leading whitespace and stops before (but does not consume) trailing whitespace. getline preserves leading whitespace and consumes (discards) its stopping character.