Cpp - Create String object and output its value

Introduction

Both the operators + and += for concatenation and the relational operators <, <=, >, >=, ==, and != are defined for string objects.

Strings can be printed with cout and the operator <<.

Demo

#include <iostream>    // Declaration of cin, cout 
#include <string>      // Declaration of class string 
using namespace std; 

int main() //w w w.j a  v a 2 s .co m
{ 
    // Defines four strings: 
    string prompt("What is your name:  "), 
            name,                  // An empty 
            line( 40, '-'),        // string with 40 '-' 
            total = "Hello ";      // is possible! 

    cout << prompt;          // Request for input. 
    getline( cin, name);     // Inputs a name in one line 

    total = total + name;     // Concatenates and 
                                    // assigns strings. 

    cout << line << endl      // Outputs line and name 
          << total << endl; 
    cout << " Your name is "  // Outputs length 
          << name.length() << " characters long!" << endl; 
    cout << line << endl; 
    return 0; 
}

Result

Related Exercise