Read first name and last name and concatenates the two into a new string. - C++ STL

C++ examples for STL:string

Description

Read first name and last name and concatenates the two into a new string.

Demo Code

#include <iostream>
#include <string>

std::string getInput(const std::string&);

int main(int argc, const char* argv[]) {
    std::string fName = getInput("Enter first name");
    std::string lName = getInput("Enter last name");

    std::cout << "Concatenation using += : ";
    std::string string1 = fName;/*from  w  w  w .ja va 2s .c o m*/
    string1 += " " + lName;

    std::cout << string1 << std::endl;

    std::cout << "Concatenation using constructor : ";
    std::string string2(fName + " " + lName);

    std::cout << string2 << std::endl;

    return 0;
}
// gets input from user
std::string getInput(const std::string& prompt) {
    std::string input;

    std::cout << prompt << ": ";
    std::cin >> input;

    return input;
}

Result


Related Tutorials