Concatenating strings - C++ STL

C++ examples for STL:string

Description

Concatenating strings

Demo Code

#include <iostream>
#include <string>

using std::string;

int main() {/*from   w  w  w .  ja v a  2s.  c om*/
  string first;                                 // Stores the first name
  string second;                                // Stores the second name

  std::cout << "Enter your first name: ";
  std::cin >> first;                            // Read first name

  std::cout << "Enter your second name: ";
  std::cin >> second;                           // Read second name

  string sentence {"Your full name is "};       // Create basic sentence
  sentence += first + " " + second + ".";       // Augment with names

  std::cout << sentence << std::endl;           // Output the sentence

  std::cout << "The string contains "           // Output its length
            << sentence.length() << " characters." << std::endl;
}

Result


Related Tutorials