The std::string Class

Chris Wild & Steven Zeil

Last modified: Jul 22, 2016
Contents:

1 Description

2 Example


#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


3 Tips - I/O

There are three approaches used for reading strings.

3.1 Read characters until a whitespace character is found

3.2 Read until the end of line.

The following program should produce the same output as the first program.

stringRead.cpp
#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;
}

3.3 Read until a special character is found.

stringRead2.cpp
#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;
}

So which method of inputting strings is the best to use?

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.