Saturday 2 April 2016

Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use char arrays and functions from the cstring header file. A sample run could look like this: Enter your first name: Flip Enter your last name: Fleming Here’s the information in a single string: Fleming, Flip

Leave a Comment


Questions:

Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use char arrays and functions from the cstring header file. A sample run could look like this:
Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

Explanation:

Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/



#include <iostream>
#include <cstring>

using namespace std;

int main() {
       cout << "Enter your first name: ";
       const int size = 20;
       char firstName[size];
       cin.getline(firstName, size - 1);
       cout << "Enter your last name: ";
       char lastName[size];
       cin.getline(lastName, size - 1);

       char name[2 * size + 2];
       strcpy(name, lastName);
       strcat(name, ", ");
       strcat(name, firstName);

       cout << "Here's the information in a single string: "
              << name << endl;
       return 0;
}



Output:


Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: